I'm trying to figure out how to override variables spawned in a derived class that are declared in a base class.
For instance:
public class construct
{
public
bool DENSITY = false;}public class mob : construct
{
DENSITY = true;
}
Is this possible with some sort of modifier or something, or do I have to change DENSITY in a method/function/procedure/sub-routine (what name do C# programmers use?) in the derived class?
AlanPosted Jul 6, 2007, 4:06 AM
FWIW, you can hide a variable declared in a parent class by declaring a similarly named variable in a child class and preceding it with the 'new' keyword.
You can still access the parent class variable either by using a cast or (within the child class code) by using the 'base' keyword. For example:
using System;
class Program
{
static void Main()
{
mob m = new mob();
Console.WriteLine(m.DENSITY); // true
Console.WriteLine(((construct)m).DENSITY); // false
Console.WriteLine(m.BaseDensity); // false
m.BaseDensity = true;
Console.WriteLine(m.BaseDensity); // true
Console.ReadLine();
}
}
public class construct
{
public bool DENSITY = false;
}
public class mob : construct
{
public new bool DENSITY = true;
public bool BaseDensity
{
get {return base.DENSITY;}
set {base.DENSITY = value;}
}
}
Jan MontanoPosted Jul 5, 2007, 10:43 PM