Hi Graeme,
I have posted some code snippets here that demonstrate what I think you are
talking about. They are only intended to demonstrate the principle you
describe and do not represent what I would consider good OO practise. It's
interesting all the same....
Consider the following two classes:
class BaseClass
{
protected int baseInt = 100;
public virtual int BaseMethod()
{
return baseInt;
}
}
class DerivedClass:BaseClass
{
}
When you do something like:
DerivedClass classBase = new DerivedClass();
Console.WriteLine(classBase.BaseMethod());
You will of course get the value of the integer (100) as defined in the base
class.
It is, however, possible to override the BaseMethod in your DerivedClass and
also declare your intention to 'hide' the value of the integer in your base
class. If you change the DerivedClass definition to:
class DerivedClass:BaseClass
{
private new int baseInt = 200;
public override int BaseMethod()
{
return baseInt;
}
}
When you run the code now you will get 200 returned as defined in the
derived class. The 'new' keyword shows your intention to hide the base class
variable.
Hope this helps - wibberlet
Development blog at
http://wibberlet.blogspot.com
"gw********@gmail.com" wrote:
Good Morning,
Ive been programming in c# for a few months now, and one of the things
i havnt quite figured out is this:
I would like to have inherited classes with their own set of
variables. I would like to use an inherited method, but instead of
using the base's variables, i want the application to instead use the
inherited methods values.
Now i've played around with the "this" keyword but havn't gotten very
far - It's also one of those problems i find it hard to google!
Hopefully someone could help me out with this?
Thanks in advance!
Graeme Woodhouse