473,378 Members | 1,543 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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

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!

Sep 14 '06 #1
3 2022
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.
Sep 14 '06 #2
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
Sep 14 '06 #3
as a note, you can do the following:

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

thanks again for the help!

ke*************@gmail.com wrote:
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.
Sep 15 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Luca | last post by:
Hi, I have a quite complex question to ask you: I have defined a base class where I would like to have a map holding pointers to member functions defined in derived classes. To be more precise...
8
by: Dave | last post by:
class base { public: base(const base &other) { // Init. here... } // Stuff }; class derived: public base { public:
1
by: Dave | last post by:
Hello NG, Regarding access-declarations and member using-declarations as used to change the access level of an inherited base member... Two things need to be considered when determining an...
5
by: Andy | last post by:
Hi all, I have a site with the following architecture: Common.Web.dll - Contains a CommonPageBase class which inherits System.Web.UI.Page myadd.dll - Contains PageBase which inherits...
2
by: Joe HM | last post by:
Hello - I have a function in a base class that I want to use the shadow'ed member variable of the derived class. Here is the code ... Public Class cCaptureBase Protected Const cDummy As...
26
by: nyathancha | last post by:
Hi, How Do I create an instance of a derived class from an instance of a base class, essentially wrapping up an existing base class with some additional functionality. The reason I need this is...
15
by: Bob Johnson | last post by:
I have a base class that must have a member variable populated by, and only by, derived classes. It appears that if I declare the variable as "internal protected" then the base class *can*...
15
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello everyone, I met with a strange issue that derived class function can not access base class's protected member. Do you know why? Here is the error message and code. error C2248:...
10
by: blangela | last post by:
If I pass a base class object by reference (likely does not make a difference here that it is passed by reference) as a parameter to a derived class member function, the member function is not...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.