473,804 Members | 2,277 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Base Class Method to use Shadow'ed member variable of Derived Class?

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.WriteLi ne(cDummy)
End Sub
End Class

Public Class cCaptureDerived
Inherits cCaptureBase

Protected Shadows Const cDummy As String = "Derived"
End Class

Sub Main()

Dim lInstance As cCaptureBase
lInstance = New cCaptureDerived

lInstance.print ()

I would like print() to print the cDummy of the cCaptureDerived class.
Is there any way of doing that? I know I could override the print() in
the derived class but I would like to keep that in the base class.

This might either be something very simple or something that should not
be done in proper OO design. Any suggestions?

Thanks!
Joe

Apr 4 '06 #1
2 1753

Joe HM wrote:
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.WriteLi ne(cDummy)
End Sub
End Class

Public Class cCaptureDerived
Inherits cCaptureBase

Protected Shadows Const cDummy As String = "Derived"
End Class

Sub Main()

Dim lInstance As cCaptureBase
lInstance = New cCaptureDerived

lInstance.print ()

I would like print() to print the cDummy of the cCaptureDerived class.
Is there any way of doing that? I know I could override the print() in
the derived class but I would like to keep that in the base class.

This might either be something very simple or something that should not
be done in proper OO design. Any suggestions?


A good rule of thumb is that a design that requires the use of
'Shadows' is doing something wrong.

Try this:

Public Class BaseClass
Protected Overridable ReadOnly Property Dummy() As String
Get
Return "base"
End Get
End Property

Public Overridable Sub Print()
Console.WriteLi ne(Me.Dummy)
End Sub

End Class

Public Class DerivedClass
Inherits BaseClass

Protected Overrides ReadOnly Property Dummy() As String
Get
Return "derived"
End Get
End Property

End Class

Module Module1

Sub Main()

Dim instance As BaseClass = New DerivedClass

instance.Print( )

Console.ReadLin e()
End Sub

End Module

--
Larry Lard
Replies to group please

Apr 4 '06 #2
D'oh ... right ... I forgot about the Property!

Thanks for the help ... and I will avoid "Shadows" in the future ...

Joe

Apr 4 '06 #3

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

Similar topics

2
1960
by: Michael Young | last post by:
I've been trying to compile code similar to the example code below, but I keep getting errors indicating that the function 'foo()' is not accessible. At first, I thought this was a bug in the compiler, but I've now tried the code against Borland C++ Builder 6.0, MS VC++ .NET, DevC++ (GCC), and Comeau's on-line compiler, and all give me similar diagnositic messages. I can make the code work if I make the "protected" items "public" in...
13
3732
by: Steve Canfield | last post by:
When compiling the following code: class Base { ... virtual void Fn(int x); virtual void Fn(Base x); }; class Derived : public Base
4
2996
by: Siemel Naran | last post by:
Can Derived class static member access protected member from base class? class Base { protected: void setvariable(int); }; class Derived : Base { public: static std::auto_ptr<Base> out(new Derived());
3
2159
by: John B | last post by:
I have a variable in a base class defined as: protected Control validationControl; How can I ensure that it is set is any dervied classes. I see that you cannot set variables to abstract. Thanks
6
2501
by: Taran | last post by:
Hi All, I tried something with the C++ I know and some things just seem strange. consider: #include <iostream> using namespace std;
14
33295
by: Dave Booker | last post by:
It looks like the language is trying to prevent me from doing this sort of thing. Nevertheless, the following compiles, and I'd like to know why it doesn't work the way it should: public class ComponentA { static string s_name = "I am the root class."; public string Name { get {return s_name;} } }
15
3857
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...
13
4939
by: Rahul | last post by:
Hi Everyone, I was just playing around virtual functions and landed up with the following, class Base1 { public: virtual void sample() { printf("base::sample\n");
10
4121
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
9594
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,...
0
10599
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10347
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
9173
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
7635
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
6863
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5531
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
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3832
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.