Userfriendly classname

  • Thread starter Thread starter cody
  • Start date Start date
C

cody

I have a base class and I want that all derived classes must have
attributes which contains entity specific stuff like a table name and a
userfriendly class name.
For specific reasons I want these things to be accessible also without
an instance of that class.

Sure, I could implement static variables and properties in each derived
class. But How could I ensure that the people deriving from my class are
doing so and most importantly, do the signatures correctly.

So if I would use reflection to retrieve the tablename of a given class
and I look for a property named TableName but the programmer of that
class unintentionally named the property NameOfTable (or whatever).

Since these informations are per class I have to make them static so
interfaces are not of use here.
 
I"m not entirely sure I follow.. but I'll give it a shot.

Basicall you want all derived classes to implement certain properties
and / or methods? To do this, you can have a base class (if you have
some functionality which you are SURE will be common to all derived
classes) or an interface.

If you go the base class route, make your base class abstract, and make
nay property or method which must be implemented by subclasses as
abstract as well.

for example:

public abstract MyBase {
public abstract string TableName { get; }
}

Anyone that derives MyBase MUST provide a property called TableName,
which is get only. (Note this is differnt than virtual, wher you
provide implementation, but the subcase MAY override the behavior).

If you go the interface route, you'd have something similar:

public interface IMyInterface {
public string TableName { get; }
}

use the interface if you need to have some behavior applied to classes
which are otherwise unrelated. For example, Float and DateTime
implement the IComparable interface, meaning they can be compared, but
don't share anything else in common.

HTH
Andy
 
Inheritance needs a virtual table in order to mandate members are implemented
or not (thus requiring an instance). Therefore C# provides no way to ensure
a deriving class implements class (static) members. The base class would
have no way of reaching derived static members anyway--without knowing about
the existence of the derived class (which defeats the purpose, IMHO).
 
For specific reasons I want these things to be accessible also
I already use base classes but these infomation must be accessible also
without instance and I do not want to store store stuff which is
specific for a class per instance.
which means that base classes are no option to solve this specific case.
 
Peter said:
Inheritance needs a virtual table in order to mandate members are implemented
or not (thus requiring an instance). Therefore C# provides no way to ensure
a deriving class implements class (static) members. The base class would
have no way of reaching derived static members anyway--without knowing about
the existence of the derived class (which defeats the purpose, IMHO).

I could simple make a static property in each of the derived classes,
each returning the class specific information I need and storing the
info in a static variable of that class. I can the use reflection (given
the type) and read the static property. no problem.
the whole problem is that I cannot ensure the derived classes provides
the property.

to get the idea look at my example what iam trying to do:

class Derived1 : Base
{
public string Info{get{return "orders";}}
}

class Derived2 : Base
{
public string Info{get{return "customers";}}
}

class Base
{

public Base[] LoadAll(Type t)
{
string tablename = t.GetProperty("Info").Invoke();
return dbconn.query("select * from "+tablename);
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top