473,796 Members | 2,444 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.SomeBa seMember()

end class

class Derived
inherits Base

'instantiate it
myStruct = new DerivedStruct

'access it
myStruct.SomeDe rivedMember

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 2060
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.SomeBa seMember()

end class

class Derived
inherits Base(Of DerivedStruct)

'instantiate it
myStruct = new DerivedStruct

'access it
myStruct.SomeDe rivedMember

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.SomeBa seMember()
myOther.SomeBas eMember()
.
.
.
end class

class Derived
inherits Base

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

'access them
myStruct.SomeDe rivedMember
myOther.SomeDer ivedMember
.
.
.

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.SomeBa seMember()

end class

class Derived
inherits Base(Of DerivedStruct)

'instantiate it
myStruct = new DerivedStruct

'access it
myStruct.SomeDe rivedMember

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
1990
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 I would like my base class to have the following member: map<string, pointer_to_member_function> myClassMap;
8
1755
by: Dave | last post by:
class base { public: base(const base &other) { // Init. here... } // Stuff }; class derived: public base { public:
1
4347
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 inherited base member's access level in the derived class: its access level in the base class and the type of inheritance (public, protected, or private). After this determination is made, the following possibilities exist for manually changing the...
5
3164
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 CommonPageBase - Contains myPage which inherits PageBase Each of these classes overrides OnInit and ties an event handler
2
1751
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 String = "Base" Overridable Sub print() Console.WriteLine(cDummy)
26
5376
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 because I am not always able to control/create all the different constructors the base class has. My problem can be described in code as follows ... /* This is the base class with a whole heap of constructors/functionality*/ public class Animal
15
3856
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* populate the variable, but the population is not *required* by the derived class (which must be the case). What would meet the requirements is if I create an abstract method in the base class that populates the member variable. In this case the...
15
3086
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: 'base::~base' : cannot access protected member declared in
10
4120
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 allowed to access the protected data members of the base object. This surprises me. Can someone explain why this is? I suspect there is a good reason and I am just having a slow day to not come up with it myself. Bob
0
9680
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9528
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10173
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10006
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9052
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7547
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5441
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5573
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4116
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.