Home All Groups Group Topic Archive Search About

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

Author
14 Sep 2006 6:19 PM
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!

Author
14 Sep 2006 6:58 PM
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

--
Mattias Sjögren [C# MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Author
14 Sep 2006 7:27 PM
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
    .
    .
    .


    ' access them
    myStruct.SomeBaseMember()
    myOther.SomeBaseMember()
    .
    .
    .


end class

class Derived
    inherits Base

    'instantiate them
    myStruct = new DerivedStruct
    myOther = new DerivedOther
    .
    .
    .

    'access them
    myStruct.SomeDerivedMember
    myOther.SomeDerivedMember
    .
    .
    .

end class

would using your generics example be able to work with something like
this, or can it only be passed one type?

thanks!

Mattias Sjögren wrote:
Show quoteHide quote
> 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
>
> --
> Mattias Sjögren [C# MVP]  mattias @ mvps.org
> http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
> Please reply only to the newsgroup.
Author
15 Sep 2006 2:37 PM
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!

keith.thornh***@gmail.com wrote:
Show quoteHide quote
> 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
>     .
>     .
>     .
>
>
>     ' access them
>     myStruct.SomeBaseMember()
>     myOther.SomeBaseMember()
>     .
>     .
>     .
>
>
> end class
>
> class Derived
>     inherits Base
>
>     'instantiate them
>     myStruct = new DerivedStruct
>     myOther = new DerivedOther
>     .
>     .
>     .
>
>     'access them
>     myStruct.SomeDerivedMember
>     myOther.SomeDerivedMember
>     .
>     .
>     .
>
> end class
>
> would using your generics example be able to work with something like
> this, or can it only be passed one type?
>
> thanks!
>
> Mattias Sjögren wrote:
> > 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
> >
> > --
> > Mattias Sjögren [C# MVP]  mattias @ mvps.org
> > http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
> > Please reply only to the newsgroup.