tags:

views:

1086

answers:

6

OK so I admit right off the top that this is a bit screwy … but it does serve a logical purpose. I’m using C# for a current project and I’m trying to find a way to override a member variable in a derived class, but access the overridden variable in a base class method. To make things more “entertaining” it would be preferable if the overridden member variable was static (this is NOT shown in the example code below).

Here is my sample code:

class baseclass
{
    protected string[] array = null;

    public string method()
    {
        string str = "";
        foreach (string x in this.array)
        {
            str += x + "  "; 
        }

        return str;
    }
}

class subclass1 : baseclass
{
    new string[] array = new string[]
    {
        "class1value1",
        "class1value2",
        "class1value3",
        "class1value4"
    };
}

class subclass2 : baseclass
{
    new string[] array = new string[]
    {
        "class2value1",
        "class2value2",
        "class2value3",
        "class2value4"
    };
}

Any thoughts as to why this doesn't work and a way to get around it?

+1  A: 

Why do you need to override the variable? Looking from your code, just setting the values would be enough, no?

Plus, static variables are tied to the class (not the instance), therefore it's not overridable on any situation.

Adrian Godong
you are creating separate class just to hold different data, +1
CrazyJugglerDrummer
+5  A: 

Is there any reason you can't use a virtual property? That would provide exactly the functionality you are looking for. It just wouldn't be a field.

protected abstract string[] array { get; }

...

protected override string[] array { get { return new string[]{"...","..."}; }}
JaredPar
I'm playing around with this right now ... you're right this might work ... still playing to be sure. Thanks for adding more confidence in my thought.
+1  A: 

Simply don't use new. Set the array in your subclass' constructor.

EDIT: with code:

class subclass1 : baseclass
{
    public subclass1()
    {
        array = new string[]
        {
            "class1value1",
            "class1value2",
            "class1value3",
            "class1value4"
        };
    }
}

class subclass2 : baseclass
{
    public subclass2()
    {
        array = new string[]
        {
            "class2value1",
            "class2value2",
            "class2value3",
            "class2value4"
        };
    }
}
McAden
this wouldn't allow me to have a static array
+1  A: 
class BaseClass
{
    public virtual string Method()
    {
        return string.Empty;
    }
}

abstract class BaseClass<T> : BaseClass where T : BaseClass<T>
{
    protected static string[] strings;

    public override string Method()
    {
        return string.Join("  ", strings);
    }
}

class Subclass1 : BaseClass<Subclass1>
{
    static Subclass1()
    {
        strings = new[] { "class1value1", "class1value2", "class1value3" };
    }
}

class Subclass2 : BaseClass<Subclass2>
{
    static Subclass2()
    {
        strings = new[] { "class2value1", "class2value2", "class2value3" };
    }
}

The important part is the generic parameter T which basically functions as an index to the string arrays.

dtb
A: 

That's all good, but what if your base class is already abstract? The C# compiler won't allow a data member in an abstract class to be called abstract or virtual a second time? Cheers.

Guy