473,411 Members | 1,982 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,411 software developers and data experts.

Userfriendly classname

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.
Jul 3 '06 #1
4 1192
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
cody wrote:
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.
Jul 3 '06 #2
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).

--
http://www.peterRitchie.com/
Microsoft MVP, Visual Development - C#
"cody" wrote:
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.
Jul 3 '06 #3
>For specific reasons I want these things to be accessible also
>without an instance of that class.
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.
Andy wrote:
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
cody wrote:
>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.
Jul 6 '06 #4
Peter Ritchie wrote:
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);
}
}
Jul 6 '06 #5

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

Similar topics

7
by: Joshua Beall | last post by:
Hi All, I have been trying to dynamically call a static member function, as follows: $className = 'MyClass'; $methodName = 'MyMethod' $result = $className::$methodName(); However, I get a...
1
by: Peter King | last post by:
if you assign multiple classes in order to keep your classes generic e.g ..classA { position.absolute; left:5 top:5 height:200 width:800 } ..classB { background-color: red} ..classC {...
4
by: Ben R. | last post by:
Between ClassName and Inherits, which attribute is set to specify the class that a page uses? I would think that would be inherits. Further, the description for ClassName is: Specifies the class...
2
by: bmgz | last post by:
I have written a script that highlights a table row when the appropriate checkbox is checked. Using element.style is a bit messy and doesn't really fulfil my needs.. I want to just be able to...
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: 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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...
0
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...

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.