[dba-VB] Storing derived classes in a list

jwcolby jwcolby at colbyconsulting.com
Wed Apr 25 12:00:40 CDT 2012


Well I figured it out, Virtual and Override keywords.

Sorry for the ring.  This is my first foray into actually building my own base classes and 
inheriting from them.

John W. Colby
Colby Consulting

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

On 4/25/2012 9:17 AM, jwcolby wrote:
>
> 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?
>



More information about the dba-VB mailing list