Search:
Namespaces
Discussions
.NET v1.1
Feedback
Static inheritence
Messages
Related Types
This message was discovered on
microsoft.public.dotnet.languages.csharp
.
Post a new message to this list...
Ren? Paw Christensen
Hi.
Considering the following code, I want this call:
Something.Method();
To return "Something".
public class BaseClass {
public static string Method() {
return <type where the method was called>.Name;
}
}
public class Something : BaseClass {
}
The code illustrates the problem, but in real life I won't return the
name of the Something type, but rather use the Something type in my
implementation in the BaseClass. Is this possible?
__________________________________________________________
If the above is not possible one solution is this:
public class BaseClass {
public static string Method(Type theType) {
return theType.Name;
}
}
public class Something : BaseClass {
}
But then I have to call it like this:
Something.Method(typeof(Something));
Live Long and Prosper
René Paw Christensen
Reply to this message...
Nick Malik
You can use a strategy pattern, but I don't know if it will work for static
members... any reason why you can't use a singleton instead of inheriting a
class with static members?
public class BaseClass {
protected abstract void PerformHiddenFunction();
public string VisibleMethod()
{
// do some stuff
this.PerformHiddenFunction();
// do more stuff
}
}
public class ChildClass : BaseClass
{
protected override void PerformHiddenFunction()
{
// do stuff that only this class would know how to do.
}
}
In the calling code:
ChildClass c = new ChildClass();
c.VisibleMethod(); // this will call the "PerformHiddenFunction"
method defined in the child class,
//even though the VisibleMethod is
defined in the
//base class and is not overridden in
the child class.
HTH
--- Nick
"Ren? Paw Christensen" <
Click here to reveal e-mail address
> wrote in message
news:
Click here to reveal e-mail address
...
[Original message clipped]
Reply to this message...
Ren? Paw Christensen
Hi Nick.
Thank you for the reply.
You are right, I can insted use non static methods. This works:
public class BaseClass {
public string Method() {
return GetType().Name;
}
}
public class Something : BaseClass {
}
This calling code returns "Something":
Something s = new Something();
s.Method(); // Returns "Something"
This requires me to create an instance of the dereived Something
class, which is possible, but often in this case I only need to work
with the Something type and reflections.
This is what I'm doing.
I'm creating a data class which represents one record in a database.
This class can perform operations like Save, Delete etc. and it also
has some general static methods like CreateTable, DeleteTable,
GetAllRecords, CountAllRecords etc.
My data class is the base class, each database table is represented
with a class inheriting the base data class. The properties which
represents a field in the database table is decorated with an
attribute, containing the field name, type, key status etc.
Example: My base data class has a static CreateTable method. It needs
to read the attributes on the type it should work with, then create
the SQL query and execute in in the database.
Currently I have to pass the type of the class to the static
CreateTable method, or I can make it non static and then create a
instance of the child class and call the CreateTable method. But then
I have these null objects, which don't represent a record in the
table.
In your example, I should implement a CreateTable method in each child
class, plus all the other methods.
Some code to illustrate my text above - I hope it helps the
understanding of what I'm doing.
public abstract class DataBase {
public static void CreateTable(type type) {
... get the attributes of the type
... create SQL query
... execute query
}
public static void DeleteTable(Type type) .....
... more static methods
public static
ArrayList
GetAllRecords(Type type) {
... get the attributes of the type
... create SQL query
... execute query and get
DataView
... foreach record in the
DataView
... use reflection to create a new instance of the
type
... set the property values using reflection and the
attributes
... return the objects in a
ArrayList
}
public void Save() { // Non static, so no problem here!
... get the attributes of the GetType() type
... use reflection to get the property values
... create SQL query
... execute query
}
public void Delete() .....
... more non static methods
}
Then each time i need a table in the database, I create and use a
class like this:
[TableAttribute("TableName")]
public MyTable : DataBase {
// Field name, type, length, is key, unique
[FieldAttribute("Guid", FieldType.Char, 50, true, false)]
public Guid Key {
...
}
[FieldAttribute("CustomerName", FieldType.Char, 255, false,
false)]
public string Name {
...
}
public string PostingAddress {
// No attribute, so this is not a field in the database
table
get {
return Name + "\r\n" + Address;
}
}
}
Now, I can have several MyTable like classes, and I only want to put
as little in them as possible. If i want all my MyTable records I use
this code:
MyTable.GetAllRecords(typeof(MyTable));
But I would like to do it like this:
MyTable.GetAllRecords();
Finally I could go non static in the DataBase class and have an empty
MyTable object:
MyTable mt = new MyTable();
mt.GetAllRecords();
Live Long and Prosper
René Paw Christensen
Reply to this message...
Nick Malik
Interesting design,
Thanks for sharing.
Did you want any feedback on the design?
--- Nick
"Ren? Paw Christensen" <
Click here to reveal e-mail address
> wrote in message
news:
Click here to reveal e-mail address
...
[Original message clipped]
Reply to this message...
Ren? Paw Christensen
Hi Nick.
Any comments and ideas are welcome.
I'm still concidering which solution to use:
* Static method with Type argument
* Non static, requires a "null/empty" object instance
* Data object scanner class 1)
1) A one instance database object, which scans the active assembly
(loaded types) for classes decorated with the table attribute, and
then add a argument to the table attribute, a Guid. Then i can do
something like this:
theDatabaseEngine.CreateTagle(<the guid>);
It then lookup the type to work with in its internal type cache
(Hashtable) of scanned and found data objects.
The guid(s) can be stored in a struct or something.
Thanx.
Live Long and Prosper
René Paw Christensen
Reply to this message...
System.Collections.ArrayList
System.Data.DataView
Ad
MBR BootFX
Best-of-breed application framework for .NET projects, developed by Matthew Baxter-Reynolds and MBR IT
Copyright © Matthew Baxter-Reynolds 2001-2008. '.NET 247 Software Development Services' is a trading style of MBR IT Solutions Ltd.
Contact Us
-
Terms of Use
-
Privacy Policy
-
www.dotnet247.com