Inheritence

G

Guest

I have an abstract class. I want to define a field and a few methods which
reference the field defined for that abstract class. The field is defined in
the abstract class but is populated in the class which inherits the class.

The only way to reference the field in the abstract class is to identify the
field as a "static" field, but when I redefine this field in the class which
inherits this class, the contents is always null when I invoke this method.
If I remove the designator of "static" from the field, then I get an error
message about "object reference is required for the nonstatic field, method
or property. Removing the "abstract" designation from the class does not
help either.

How can I create a method in the base class which reference a field in the
inherited class?

Thanks in advance for your assistance!
 
B

Bob Powell [MVP]

I'm assuming you have not used protected access on your field... Maybe
you should.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
L

Leon Lambert

That is what I would do also but the odd thing is that FXCop doesn't
like this. It wants you to make it a property and have the child class
access it that way. I don't like disabling rules in FXCop but find
myself turning this one off a lot.
 
B

Brian Gideon

Leon,

Well, making it a property is the right thing to do. The reasoning is
the same as it is for public properties of private fields. Protected
members are considered part of the published interface for the class
so practicing good design is necessary for them too.

Brian
 
S

Stoitcho Goutsev \(100\)

Jim,

Can you post compilable sample that demonstrates your problem?
 
B

Bjorn Abelli

JimHeavey said:
I have an abstract class. I want to define a field and a few methods
which reference the field defined for that abstract class. The field
is defined in the abstract class but is populated in the class which
inherits the class.

abstract class BaseClass
{
protected string myField;
}

class InheritedClass : BaseClass
{
public InheritedClass()
{
myField = "Populated";
}
}

The only way to reference the field in the abstract class
is to identify the field as a "static" field,

No, that's not the *only* way.

Members defined in the base class are inherited by the subclass, i.e.
objects of the subclass "contains" the members it has inherited.

If you want to reference them, you have to be able to *reach* them, e.g.
with a public modifier or preferable with a protected modifier.
but when I redefine this field in the class which
inherits this class, the contents is always null
when I invoke this method.

If you "redefine" the member, you don't use the inherited one, but rather
"hide" it.
If I remove the designator of "static" from the field, then
I get an error message about "object reference is required
for the nonstatic field, method or property.

This shows that you are trying to reach a non-static member from a static
context.

Populate the inherited members from a non-static context instead. I gave one
example above, but can give you another:

abstract class BaseClass
{
protected string myField;
}

class InheritedClass : BaseClass
{
public void PopulateMyField(string txt)
{
myField = txt;
}
}
How can I create a method in the base class which reference
a field in the inherited class?

Now you're turning your question completely around.

You should not try to access members defined in a subclass from its base
class, but perhaps you only wrote that sentence the wrong way around.



/// Bjorn A
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top