how to make base and derived class use the same member variable?

K

keith.thornhill

hey all, got a problem here using Visual basic .net 2005

i have two pairs of base/derived classes. lets call them Base/Derived
and BaseStruct/DerivedStruct.

i want to be able to instantiate a DerivedStruct in my Derived class,
but have the instance of it be accessible from both the Base class (as
BaseStruct) and Derived class (as DerivedStruct)

this is the kind of code i'm looking to be able to use

----------------------------------

class Base

'declare it
protected myStruct as BaseStruct

' access it
myStruct.SomeBaseMember()

end class

class Derived
inherits Base

'instantiate it
myStruct = new DerivedStruct

'access it
myStruct.SomeDerivedMember

end class

----------------------------------

i can get something similar working, but only if, in my derived class,
i use CType() to cast myStruct to the Derived type before i try to
access any derived members of the DerivedStruct

i REALLY would like the >compiler< to see that when i reference
myStruct from within the Base, to give me access to the BaseStruct
members and when i reference myStruct from within Derived, to give me
access to the DerivedStruct members. and for both examples, i want it
all to be accessing one instance of that DerivedStruct

i've tried puting "protected myStruct as DerivedStruct = new
DerivedStruct" in my Derived class, but this shadows the base myStruct
so they aren't the same thing in memory.

is this possible? thanks!
 
M

Mattias Sjögren

Keith,
i REALLY would like the >compiler< to see that when i reference
myStruct from within the Base, to give me access to the BaseStruct
members and when i reference myStruct from within Derived, to give me
access to the DerivedStruct members. and for both examples, i want it
all to be accessing one instance of that DerivedStruct

Can't you just add another field (of type DerivedStruct) to the
Derived class and make it reference the same object as myStruct in the
Base class? (I assume BaseStruct and DerivedStruct are classes and not
structures, despite their names. Otherwise DerivedStruct wouldn't be
able to derive from BaseStruct).

Another option is to use generics

class Base(Of T As {BaseStruct})

'declare it
protected myStruct as T

' access it
myStruct.SomeBaseMember()

end class

class Derived
inherits Base(Of DerivedStruct)

'instantiate it
myStruct = new DerivedStruct

'access it
myStruct.SomeDerivedMember

end class


Mattias
 
K

keith.thornhill

thanks for replying.

first: yes they are all classes, the naming scheme was just the first
thing i thought of when trying to make my example clear :)

the reason why i didn't just create a new field in Derived (of type
DerivedStruct) to point to myStruct is that i would like the name by
which i access them to be the same in Base and Derived. i suppose that
solution would be a fallback if i can't get my desired functionality
working.

on your generics example, i haven't tried to implement it yet, but how
well would it scale?

let say i had more base/derived pairs which i wanted to work with:

class Base

'declare them
protected myStruct as BaseStruct
protected myOther as BaseOther
 
K

keith.thornhill

as a note, you can do the following:

class Base(Of T1 As BaseStruct, Of T2 as BaseOther, ...)

thanks again for the help!
 

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

Top