473,671 Members | 2,504 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Static inheritence

Hi.

Considering the following code, I want this call:

Something.Metho d();

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.Metho d(typeof(Someth ing));

Live Long and Prosper
René Paw Christensen
Nov 16 '05 #1
5 1474
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 PerformHiddenFu nction();
public string VisibleMethod()
{
// do some stuff
this.PerformHid denFunction();
// do more stuff
}
}

public class ChildClass : BaseClass
{
protected override void PerformHiddenFu nction()
{
// 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 "PerformHiddenF unction"
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.goo gle.com...
Hi.

Considering the following code, I want this call:

Something.Metho d();

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.Metho d(typeof(Someth ing));

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(typ e type) {
... get the attributes of the type
... create SQL query
... execute query
}
public static void DeleteTable(Typ e type) .....
... more static methods
public static ArrayList GetAllRecords(T ype 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( "CustomerNa me", 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.GetAllR ecords(typeof(M yTable));

But I would like to do it like this:

MyTable.GetAllR ecords();

Finally I could go non static in the DataBase class and have an empty
MyTable object:

MyTable mt = new MyTable();
mt.GetAllRecord s();
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.goo gle.com...
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(typ e type) {
... get the attributes of the type
... create SQL query
... execute query
}
public static void DeleteTable(Typ e type) .....
... more static methods
public static ArrayList GetAllRecords(T ype 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( "CustomerNa me", 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.GetAllR ecords(typeof(M yTable));

But I would like to do it like this:

MyTable.GetAllR ecords();

Finally I could go non static in the DataBase class and have an empty
MyTable object:

MyTable mt = new MyTable();
mt.GetAllRecord s();
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:

theDatabaseEngi ne.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.goo gle.com...
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:

theDatabaseEngi ne.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
4139
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 capabilities
6
7929
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
2619
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 one of the member functions. Show that the other is hidden in the derived class. The simple test code I wrote seems NOT to confirm this. The question
7
1980
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 inheritence. One more article I read was Allen holub's "Why extends is evil". http://www.javaworld.com/javaworld/jw-08-2003/jw-0801-toolbox.html One general advice I found was that - extending behaviour is better done with interfaces and...
15
6579
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 either has access only to static members of the class (ie. assuming no object of the class is in scope - neither by arguments recieved nor by local declarations). Any static member function like this: //accessing static member i static void...
5
24216
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 NullObject.Instance; } // !!! Wrong, C# not support ? }
5
2120
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 disagreement about whether is essential in C++". Are there any disadvantages of using multiple inheritence?
13
20764
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 & I don't want to copy + paste code. How can I inherit those member variables? Thanks
5
1828
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 quite a heated debate about purity of OO design that just did my head in a bit. In a nutshell. Can a static method 'know' what class it's defined in and return that type information?
0
8402
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8605
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6237
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5703
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4227
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4416
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2819
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2062
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1816
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.