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