[dba-VB] Storing derived classes in a list

Salakhetdinov Shamil mcp2004 at mail.ru
Wed Apr 25 11:54:58 CDT 2012


Hi John --

Quick & Dirty:

if (lstClsSQLFlagGridUpdate[gridValues.pFieldName] is clsFlagDte)
{
   ....
}
else if (lstClsSQLFlagGridUpdate[gridValues.pFieldName] is clsFlagInt)
{
  ...
}

As usual there should be many other (more elegant) (and OOP) ways to achieve the same results...

Thank you.

-- Shamil


Wed, 25 Apr 2012 09:17:42 -0400 от jwcolby <jwcolby at colbyconsulting.com>:
> 
> 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
> 
> _______________________________________________
> dba-VB mailing list
> dba-VB at databaseadvisors.com
> http://databaseadvisors.com/mailman/listinfo/dba-vb
> http://www.databaseadvisors.com
> 
> 



More information about the dba-VB mailing list