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

Inheritance Part 2

I have a class (base) which has 1 field (_MyField) and 1 method
(DoSomething() ).

This class is inherited by another class (MyClass). This class sets
_MyField to a value and then invokes DoSomething. When Do Something is
invoked, _MyField is always null. Why?

public abstract base
{
protected string[] _MyField;
public base()
{
}
public string DoSomething()
{
return _MyField[0];
}
}

public class MyClass : base
{
private string[] _MyField = { "one", "Two", "Three" };
public MyClass()
{
Console.Writeline ( DoSomething() );
}

}
Feb 7 '07 #1
2 1016
JimHeavey <Ji*******@discussions.microsoft.comwrote:
I have a class (base) which has 1 field (_MyField) and 1 method
(DoSomething() ).

This class is inherited by another class (MyClass). This class sets
_MyField to a value and then invokes DoSomething. When Do Something is
invoked, _MyField is always null. Why?

public abstract base
{
protected string[] _MyField;
public base()
{
}
public string DoSomething()
{
return _MyField[0];
}
}

public class MyClass : base
{
private string[] _MyField = { "one", "Two", "Three" };
public MyClass()
{
Console.Writeline ( DoSomething() );
}
}
You've got two _MyField variables - the one in the derived class hides
the one in the base class, but doesn't replace it. In other words, the
line of your description: "This class sets _MyField to a value and then
invokes DoSomething" is inaccurate.

Try this:

public class MyClass : base
{
public MyClass()
{
_MyField = new string[] { "one", "Two", "Three" };
Console.Writeline ( DoSomething() );
}
}

(Oh, and please try to not name your classes with C# keywords...)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 7 '07 #2

"JimHeavey" <Ji*******@discussions.microsoft.comwrote:
>I have a class (base) which has 1 field (_MyField) and 1 method
(DoSomething() ).

This class is inherited by another class (MyClass). This class sets
_MyField to a value and then invokes DoSomething. When Do Something is
invoked, _MyField is always null. Why?
Because you *don't* populate the _MyField inherited from the base class, but
the array _MyField in MyClass, which is *hiding* the inherited field.

Simply don't redefine it, e.g.:

public class MyClass : base
{
public MyClass()
{
_MyField = { "one", "Two", "Three" };
Console.Writeline ( DoSomething() );
}
}
/// Bjorn A
Feb 11 '07 #3

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

Similar topics

11
by: Ricky Romaya | last post by:
Hi, Are there any ways to get multiple inheritace in PHP4? For example, I have 3 parent class, class A, B, and C. I want class X to inherit all those 3 classes. Consider merging those 3 classes...
12
by: Steve Jorgensen | last post by:
The classing Visual Basic and VBA support for polymorphism, let's face it, is a bit on the weak side, and built-in support for inheritance is non-existent. This little essay is about some patterns...
22
by: Matthew Louden | last post by:
I want to know why C# doesnt support multiple inheritance? But why we can inherit multiple interfaces instead? I know this is the rule, but I dont understand why. Can anyone give me some concrete...
45
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using...
6
by: VR | last post by:
Hi, I read about Master Pages in ASP.Net 2.0 and after implementing some WinForms Visual Inheritance I tryed it with WebForms (let's say .aspx pages, my MasterPage does not have a form tag itself...
31
by: John W. Kennedy | last post by:
I quite understand about prototypes and not having classes as such, but I happen to have a problem involving blatant is-a relationships, such that inheritance is the bloody obvious way to go. I can...
60
by: Shawnk | last post by:
Some Sr. colleges and I have had an on going discussion relative to when and if C# will ever support 'true' multiple inheritance. Relevant to this, I wanted to query the C# community (the...
6
by: burningodzilla | last post by:
Hi all - I'm preparing to dive in to more complex application development using javascript, and among other things, I'm having a hard time wrapping my head around an issues regarding "inheritance"...
23
by: Dave Rahardja | last post by:
Since C++ is missing the "interface" concept present in Java, I've been using the following pattern to simulate its behavior: class Interface0 { public: virtual void fn0() = 0; };
3
by: Leo Seccia | last post by:
Hello everyone, I have a c# project with a sql server database. I have a number of lookup tables in my database which I successfully managed to import into my LINQ dataclasses. eg. Table:...
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
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
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
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...
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
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...
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,...
0
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...

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.