DeclanMcD
DeclanMcD

Reputation: 1586

C# overrididing static variables

I would like to do the following but it doesn't quite work. What am I missing or where am I going wrong?

I have a base class (ValueClass) and this is inherited by 2 other classes (ClassA and ClassB).

In my code I would like to do the following:

var _value = ClassB.Get(1);

and get the ValuePair(){Key= 1, Value= 999999999M} returned but when it is queried in the Get function the _Values is null.

How can I achieve this?

public class ValuePair
{
    public int Key;
    public decimal Value;
}

public class ValueClass
{
    private static List<ValuePair> _Values { get; set; }

    public static decimal? Get(int? key)
    {
        var _result = _Values.Where(i => i.Key == key).FirstOrDefault();
        return _result != null ? _result.Value : (decimal?)null;
    }
}

public class ClassA : ValueClass
{
    private static List<ValuePair> Values=new List<ValuePair>(){    
        new ValuePair(){Key= 1, Value= 50M},    
        new ValuePair(){Key= 2, Value= 100M}
        };
}

public class ClassB : ValueClass
{
    private static List<ValuePair> _Values=new List<ValuePair>(){   
        new ValuePair(){Key= 1, Value= 999999999M},             
        new ValuePair(){Key= 2, Value= 25M}             
    };
}

Upvotes: 0

Views: 175

Answers (2)

One slight improvement I might suggest to Ian's abstract solution, if you want to maintain the static nature of your child class values:

public class ClassA : ValueClass
{
    static List<ValuePair> Values = null;
    protected override List<ValuePair> Values
    {
        get
        {
            if (Values == null)
            {
                Values = new List<ValuePair>(){    
                new ValuePair(){Key= 1, Value= 50M},    
                new ValuePair(){Key= 2, Value= 100M}
                };
            }
            return Values;
        }
    }
}

Upvotes: 0

Ian
Ian

Reputation: 34489

You can't override static methods/properties/fields. You'll have to remove the static for this to work. A better approach may be to use an Abstract class instead.

The Abstract keyword forces all concrete implementations of the class to implement the given method/property (a bit like an Interface).

public abstract class ValueClass
{
    protected abstract List<ValuePair> Values { get; }

    public decimal? Get(int? key)
    {
        var _result = Values.Where(i => i.Key == key).FirstOrDefault();
        return _result != null ? _result.Value : (decimal?)null;
    }
}

public class ClassA : ValueClass
{
    protected override List<ValuePair> Values
    {
        get { return new List<ValuePair>(){    
            new ValuePair(){Key= 1, Value= 50M},    
            new ValuePair(){Key= 2, Value= 100M}
        };
     }
   }
}

public class ClassB : ValueClass
{
    protected override List<ValuePair> Values 
    { 
        get { return new List<ValuePair>(){   
             new ValuePair(){Key= 1, Value= 999999999M},             
            new ValuePair(){Key= 2, Value= 25M}             
        };
    }
  }
}

Upvotes: 5

Related Questions