public class Foo
{
public const int type = 1;
}
Why can't I do this? Is there a reason for this or am I trying to access the constant incorrectly?
new Foo().type;
I know what I can do Foo.type, but given my scenario, I cannot do this. For example, if I have two classes that inherit the base class as follows:
public class Base
{
...
}
public class Foo : Base
{
public const int type = 0;
}
public class Bar : Base
{
public const int type = 1;
}
public static void printType(Base b)
{
Console.WriteLine(b.type);
}
I would like to get the property of a typeclass dispatched via the printType () function, but I cannot, because I can only access typefrom the class, not the object.
Work around would be
if(b is Foo){
Console.Write(Foo.type);
}elseif....
but it seems silly and unviable if you have many subclasses Base
<h / ">
Decision
I ended up using readonlyinstead constas follows:
public readonly int type = 0;