I want to declare a variable in my class, that cannot be changed later like this:
obj myobj=new obj()
myobj.CONSTANT_VAR="Changed value" //ERROR!!
...but whose value can be accessed like:
Console.WriteLine(myobj.CONSTANT_VAR)
I tried the following:
public class obj{
public int a, b;
public const string CONSTANT_VAR;
public obj(int x,int y){
a=x;
b=y;
CONSTANT_VAR=1/(a*((""+a).Length)+3/(b*((""+b).Length)).ToString();
}
public int do(){
return this.a+this.b-(CONSTANT_VAR).Length;
}
}
class DriverClass(){
static void Main(){
obj myObj=new obj(2,3);
myObj.a=34;
myObj.b=35;
myObj.CONSTANT_VAR="changed ur string lol"; //i want it to print error
Console.WriteLine(CONSTANT_VAR); //no error
Console.WriteLine(myObj.add());
}
}
But i instead get the following error message:
constants must have a value assigned
But i dont want to assign it a value beforehand..... What do i do?