473,406 Members | 2,620 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,406 software developers and data experts.

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.WriteLine(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 1733

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.WriteLine(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.WriteLine(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.ReadLine()
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
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...
13
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
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...
3
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. ...
6
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
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...
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*...
13
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
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...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
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,...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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,...

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.