Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have abstract and overriding constants in C#?

My code below won't compile. What am i doing wrong? I'm basically trying to have a public constant that is overridden in the base class.

public abstract class MyBaseClass {   public abstract const string bank = "???"; }  public class SomeBankClass : MyBaseClass {   public override const string bank = "Some Bank"; } 

Thanks as always for being so helpful!

like image 640
Chris Avatar asked Mar 09 '10 05:03

Chris


People also ask

Can you override a constant?

You can't do that. Show activity on this post. If a constant has a variable value, it's not a constant anymore. Static fields and methods are not polymorphic.

Can abstract class have data members in C#?

Abstract class members marked as abstract must be implemented by derived classes. An abstract class is a special type of class that cannot be instantiated and acts as a base class for other classes. Abstract class members marked as abstract must be implemented by derived classes.

Can abstract class have private methods in C#?

If a method of a class is private, you cannot access it outside the current class, not even from the child classes of it. But, incase of an abstract method, you cannot use it from the same class, you need to override it from subclass and use. Therefore, the abstract method cannot be private.

Can abstract class have access modifiers in C#?

An Abstract class can have access modifiers like private, protected, and internal with class members. But abstract members cannot have a private access modifier. An Abstract class can have instance variables (like constants and fields). An abstract class can have constructors and destructors.


2 Answers

If your constant is describing your object, then it should be a property. A constant, by its name, should not change and was designed to be unaffected by polymorphism. The same apply for static variable.

You can create an abstract property (or virtual if you want a default value) in your base class:

public abstract string Bank { get; } 

Then override with:

public override string Bank { get { return "Some bank"; } } 
like image 70
Pierre-Alain Vigeant Avatar answered Oct 03 '22 03:10

Pierre-Alain Vigeant


What you are trying to do cannot be done. static and const cannot be overridden. Only instance properties and methods can be overridden.

You can turn that bank field in to a property and market it as abstract like the following:

public abstract string Bank { get; } 

Then you will override it in your inherited class like you have been doing

public override string Bank { get { return "Charter One"; } } 

Hope this helps you. On the flip side you can also do

public const string Bank = "???"; 

and then on the inherited class

public const string Bank = "Charter One"; 

Since static and const operate outside of polymorphism they don't need to be overriden.

like image 39
Nick Berardi Avatar answered Oct 03 '22 05:10

Nick Berardi