[dba-VB] Storing derived classes in a list

jwcolby jwcolby at colbyconsulting.com
Wed Apr 25 08:17:42 CDT 2012


I have a dictionary which is typed to clsFlag

private Dictionary<string, clsFlag> lstClsSQLFlagGridUpdate = new Dictionary<string, clsFlag>();

In my program I never actually instantiate clsFlag but rather derived classes which inherit clsFlag 
and use its functionality.  So I am actually instantiating and storing clsFlagDte, clsFlagInt etc.

In all other regards this seems to be working well.  When I read a flag out I simply cast the flag 
to its real type and off I go. However there is one specific place in my code where the code does 
not know the type and thus leaves it as an object:

public void mGridUpdate(clsGridValues gridValues)
{
	lstClsSQLFlagGridUpdate[gridValues.pFieldName].pValue = gridValues.pValue;
}

IOW lstClsSQLFlagGridUpdate[gridValues.pFieldName] is a pointer to an object in the list, and that 
object is *not* clsFlag.

However in this case the .pValue is calling pValue in the base class which is not what I intend. 
.PValue in the base class doesn't do anything.

So basically I need to discover what subclass type is in the list and get an instance of that 
subclass (a pointer to the object in the list cast to the correct type), then update pValue of that 
pointed to object.

I have no idea how to do that.  I could do something klutzy like creating a "type variable" in each 
subclass and set that to the correct type in the constructor, and then in the method above use a 
switch to cast based on that "type variable" but that seems ugly.

Is there some syntax to tell this thing to call the pValue method in the subclass instead of the 
base object?

-- 
John W. Colby
Colby Consulting

Reality is what refuses to go away
when you do not believe in it



More information about the dba-VB mailing list