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