473,394 Members | 1,641 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,394 software developers and data experts.

C# Pattern for shadowing member variables?

Hi all.

SITUATION
================
1. I have a base class with a member variable that's an object
2. I have several classes that inherit from the base class.
3. There are several methods in the base class that modify the member
variable 4. I would like to have the inherited classes override the
member variable with a new var that's a subclass of the parent's
member variable
5. When someone instantiates the subclass and calls a methods that is
only implemented by the parent class, I like that parent method to
reference the overridden member variable.

EXAMPLE
===================================
class AAA
{
public YYY m_var = new YYY();

public void TestMethod() {

//HERE I WAS HOPING THAT localvar WOULD REFERENCE A ZZZ
CLASS
//INSTANCE HERE I WAS HOPING THAT BY USING THE this KEYWORD
THAT
//THE RUNTIME CLR WOULD RECOGNIZE THAT this IS OF TYPE ZZZ
SO THE
//m_var RETURNED WOULD BE OF TYPE ZZZ.

YYY localvar = this.m_var;

//BUT IT DOESN'T....

Type t = localvar.GetType();
Debug.Print(t.FullName);
}
}

class BBB : AAA
{
public ZZZ m_var = new ZZZ();
}

class YYY
{
}

class ZZZ : YYY
{
}
Any recommendations or new patterns that might help out?
TIA
Nov 16 '05 #1
2 7829
There's two approaches to solving your problem.

1) Mark the field with protected in the base class, create a constructor on
the sub class that initilizes the field.

class Mammal
{
protected string Name = "Mammal";

public void MakeNoise()
{
Console.WriteLine("The mammal {0} made some noise", this.Name);
}
}

class Mammoth : Mammal
{
public Mammoth()
{
this.Name = "Mammoth";
}
}

2) Create virtual properties and access the variable through them,
properties behave as methods and could be marked as virtual and then be
overriden in the sub class.

--
Patrik Löwendahl [C# MVP]
www.cshrp.net - "Elegant code by witty programmers"

"Mark Sisson" <ma**@corporatedigital.com> wrote in message
news:88**************************@posting.google.c om...
Hi all.

SITUATION
================
1. I have a base class with a member variable that's an object
2. I have several classes that inherit from the base class.
3. There are several methods in the base class that modify the member
variable 4. I would like to have the inherited classes override the
member variable with a new var that's a subclass of the parent's
member variable
5. When someone instantiates the subclass and calls a methods that is
only implemented by the parent class, I like that parent method to
reference the overridden member variable.

EXAMPLE
===================================
class AAA
{
public YYY m_var = new YYY();

public void TestMethod() {

//HERE I WAS HOPING THAT localvar WOULD REFERENCE A ZZZ
CLASS
//INSTANCE HERE I WAS HOPING THAT BY USING THE this KEYWORD
THAT
//THE RUNTIME CLR WOULD RECOGNIZE THAT this IS OF TYPE ZZZ
SO THE
//m_var RETURNED WOULD BE OF TYPE ZZZ.

YYY localvar = this.m_var;

//BUT IT DOESN'T....

Type t = localvar.GetType();
Debug.Print(t.FullName);
}
}

class BBB : AAA
{
public ZZZ m_var = new ZZZ();
}

class YYY
{
}

class ZZZ : YYY
{
}
Any recommendations or new patterns that might help out?
TIA

Nov 16 '05 #2
q
From MSDN

Name hiding through inheritance occurs when classes or structs redeclare
names that were inherited from base classes. This type of name hiding
takes one of the following forms:

A constant, field, property, event, or type introduced in a class or
struct hides all base class members with the same name.
A method introduced in a class or struct hides all non-method base class
members with the same name, and all base class methods with the same
signature (method name and parameter count, modifiers, and types).
An indexer introduced in a class or struct hides all base class indexers
with the same signature (parameter count and types).
The rules governing operator declarations (Section 10.9) make it
impossible for a derived class to declare an operator with the same
signature as an operator in a base class. Thus, operators never hide one
another.

Contrary to hiding a name from an outer scope, hiding an accessible name
from an inherited scope causes a warning to be reported. In the example

class Base
{
public void F() {}
}
class Derived: Base
{
public void F() {} // Warning, hiding an inherited name
}
the declaration of F in Derived causes a warning to be reported. Hiding
an inherited name is specifically not an error, since that would
preclude separate evolution of base classes. For example, the above
situation might have come about because a later version of Base
introduced an F method that wasn't present in an earlier version of the
class. Had the above situation been an error, then any change made to a
base class in a separately versioned class library could potentially
cause derived classes to become invalid.

The warning caused by hiding an inherited name can be eliminated through
use of the new modifier:

class Base
{
public void F() {}
}
class Derived: Base
{
new public void F() {}
}
The new modifier indicates that the F in Derived is "new", and that it
is indeed intended to hide the inherited member.

A declaration of a new member hides an inherited member only within the
scope of the new member.

class Base
{
public static void F() {}
}
class Derived: Base
{
new private static void F() {} // Hides Base.F in Derived only
}
class MoreDerived: Derived
{
static void G() { F(); } // Invokes Base.F
}
In the example above, the declaration of F in Derived hides the F that
was inherited from Base, but since the new F in Derived has private
access, its scope does not extend to MoreDerived. Thus, the call F() in
MoreDerived.G is valid and will invoke Base.F.


Mark Sisson wrote:
Hi all.

SITUATION
================
1. I have a base class with a member variable that's an object
2. I have several classes that inherit from the base class.
3. There are several methods in the base class that modify the member
variable 4. I would like to have the inherited classes override the
member variable with a new var that's a subclass of the parent's
member variable
5. When someone instantiates the subclass and calls a methods that is
only implemented by the parent class, I like that parent method to
reference the overridden member variable.

EXAMPLE
===================================
class AAA
{
public YYY m_var = new YYY();

public void TestMethod() {

//HERE I WAS HOPING THAT localvar WOULD REFERENCE A ZZZ
CLASS
//INSTANCE HERE I WAS HOPING THAT BY USING THE this KEYWORD
THAT
//THE RUNTIME CLR WOULD RECOGNIZE THAT this IS OF TYPE ZZZ
SO THE
//m_var RETURNED WOULD BE OF TYPE ZZZ.

YYY localvar = this.m_var;

//BUT IT DOESN'T....

Type t = localvar.GetType();
Debug.Print(t.FullName);
}
}

class BBB : AAA
{
public ZZZ m_var = new ZZZ();
}

class YYY
{
}

class ZZZ : YYY
{
}
Any recommendations or new patterns that might help out?
TIA


Nov 16 '05 #3

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

Similar topics

17
by: Medi Montaseri | last post by:
Hi, Given a collection of similar but not exact entities (or products) Toyota, Ford, Buick, etc; I am contemplating using the Abstraction pattern to provide a common interface to these products....
2
by: cppaddict | last post by:
I have a design question which I am posting here because the implementation will be in C++ and I think there may be C++ specific language constructs (eg, friends) that might be relevant to the...
0
by: nin234ATIyahoo.com | last post by:
I am wondering what is a good design pattern in terms of C++ class relationship for the problem I am trying to solve. template<class DbKey, class DbVal, class DbTag> class srvBerkDB : public Db...
0
by: Tony Wong | last post by:
I am trying to implement the Singleton Pattern for a assembly (class) which control a common resource on my computer. I need the Singleton behavior within a single process which contain multiple...
1
by: Remco | last post by:
Hi, Let me try to simply explain my questions. I've created a portal site with different types of users, e.g. Portal Administrators and Normal Users. One base class SessionUser (has a enum...
5
by: Dave Taylor | last post by:
I'm trying to derive a class from a typed DataSet to add some methods to one of the DataTables. I would like to keep the names the same in the derived class as in the base class, so I have used...
11
by: donet programmer | last post by:
Can somebody comment on the usage of the singleton pattern for maintaining session variables?? Thanks
9
by: raylopez99 | last post by:
Here are two different ways of achieving a mediator pattern: the first, using circular references (for lack of a better way to describe it), but not using delegates, with the second using...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
0
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...

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.