Mack
Mack

Reputation: 179

Override Base Class Static Variable

I am attempting to override a base classes const/static variable. But I get the compiler error:

'Arc.CLASS_NAME' hides inherited member `Element.CLASS_NAME'. Use the new keyword if hiding was intended

Is there a way to override this sort of variable? Maybe I need to change the data type to public virtual static readonly?

public class Element {
    public const string CLASS_NAME = "___ELEMENT___";

    ...
}

public class Arc : Element {
    public const string CLASS_NAME = "___ARC___";

    // Problem is: CLASS_NAME needs to be const to be used as a default argument
    public Arc(uint nUTMZone=DEF_UTM_ZONE, string nName=CLASS_NAME) : base(nUTMZone, nName) {

    }
}

Upvotes: 0

Views: 656

Answers (2)

brainless coder
brainless coder

Reputation: 6430

Not sure what you meant by -

logically, interpret this as making the variable private (not overriding which is what it really means from the answer below) and I need this variable to be public

This seems contradictory because -

  1. If you really need it as public (which I think you are referring to the Parent Class Variable) then what is the point of overriding it? or creating a same variable with the same name?

  2. If you want to override the variable with one of the same name then either go with new or the solution @Dmtry gave. But there is a benefit to using new. You can change the variable signature as you wish. For example the following is a perfectly compiling code -

    public class Element {
        public const string CLASS_NAME = "___ELEMENT___";
    
    }
    
    public class Arc : Element {
        private new readonly string CLASS_NAME = "___ARC___";
    }
    
  3. When they are instance variable, even though the variable hides the parent, you can still refer the parent variable by using either type casting or base reference as below -

    public class Element
    {
        public string CLASS_NAME = "___ELEMENT___";
    
    }
    
    public class Arc : Element
    {
        public new string CLASS_NAME = "___ARC___";
    }
    public class Program
    {
        public static void Main(string[] args)
        {
            Arc a = new Arc();
    
            Console.WriteLine(a.CLASS_NAME);
            Console.WriteLine((a as Element).CLASS_NAME);
    
            Console.ReadLine();
        }
    }
    
  4. But again you are using static variable, so the only way you can access them is with ClassName therefore the point of overriding should not be a problem, because you will always access through class and never by instance, so you will always know which variable you are using.-

    public class Element
    {
        public const string CLASS_NAME = "___ELEMENT___";
    
    }
    
    public class Arc : Element
    {
        public new const string CLASS_NAME = "___ARC___";
    }
    public class Program
    {
        public static void Main(string[] args)
        {
            Arc a = new Arc();
    
            Console.WriteLine(Arc.CLASS_NAME);
            Console.WriteLine(Element.CLASS_NAME);
    
            Console.ReadLine();
        }
    }
    

Upvotes: 0

quantdev
quantdev

Reputation: 23813

No, you cannot override it; constants are not virtual members (the compiler basically replaces constants by their values during the compilation).

If your intent is to hide the base class field, then do what the compiler says: add the new keyword:

new public const string CLASS_NAME = "___ARC___";

Upvotes: 1

Related Questions