473,386 Members | 1,821 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Static inheritence

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
Nov 16 '05 #1
5 1462
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" <sp***@rpc-scandinavia.dk> wrote in message
news:ec**************************@posting.google.c om...
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

Nov 16 '05 #2
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
Nov 16 '05 #3
Interesting design,
Thanks for sharing.

Did you want any feedback on the design?

--- Nick

"Ren? Paw Christensen" <sp***@rpc-scandinavia.dk> wrote in message
news:ec**************************@posting.google.c om...
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

Nov 16 '05 #4
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
Nov 16 '05 #5
It just seems like you are creating a fairly complicated method of driving
the creation of database tables from code, when database tables are only
created from code ONCE, and then have to be tweaked after you are done
anyway, to get the declarative referential integrity right.

Also, the code that you were having some trouble with, at the start of this
thread, is code that deals with Insert, Update, and Delete methods on the
table, and there is a perfectly servicable object that has working code for
this: the DataTable object. Why not just encapsulate the DataTable object,
or even the DataAdapter, under your object. You won't need to code anything
for the table operations... and you can focus on the DDL extensions that the
DataSet object doesn't already provide.

In other words, why write a new object when you can extend an existing one?

Anyway, good luck, whatever road you choose.

--- Nick

"Ren? Paw Christensen" <sp***@rpc-scandinavia.dk> wrote in message
news:ec**************************@posting.google.c om...
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

Nov 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: John | last post by:
Hi, I am trying to create a class heirarchy similar to the following: // base interface class ICar { public: virtual void start() = 0; }; // add members to that interface, but retain base...
6
by: Michael Klatt | last post by:
I am working on a library which uses the GKS graphics package. I have a Gks object which opens the GKS subsystem in its constructor: // Gks.hpp class Gks { public : Gks(); };
3
by: exits funnel | last post by:
Hello, One of the problems at the end of Chapter 14 in Bruce Eckel's thinking in C++ reads as follows: Create a class with two static member functions. Inherit from this class and redefine...
7
by: preetam | last post by:
Hi, This question is more towards design than towards c++ details. By looking at books on design patterns and various google threads on the same topic, I see that composition is favoured to...
15
by: Samee Zahur | last post by:
Question: How do friend functions and static member functions differ in terms of functionality? I mean, neither necessarily needs an object of the class to be created before they are called and...
5
by: TruongLapVi | last post by:
Hi, Why C# does not support Interface static member ? Some time I want implement NullObject Pattern: public interface INullObject { public static INullObject Null { get { return...
5
by: Neelesh Bodas | last post by:
This might be slightly off-topic. Many books on C++ consider multiple inheritence as an "advanced" concept. Bruce Eckel says in TICPP, volume 2 that "there was (and still is) a lot of...
13
by: learning | last post by:
Hi I have a static class written by other team which encapsulates a database instance. but I need to extend it to incldue other things. I know that C# static class is sealed and can;t be inherited...
5
by: DamienS | last post by:
Hi, I have a static method in a class and I need to be able to return a reference to "this". Googling around, I found a heap of discussions of the pros/cons of "abstract static" etc. It was...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.