-2

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?

4
  • It would really help if you'd post code which is valid other than the problem you're facing - your class declarations are invalid to start with. It's also important to follow naming conventions, even when it's just sample code.
    – Jon Skeet
    Commented Jun 14, 2021 at 15:23
  • 3
    First of all, your code shouldn't compile, since you cannot use "()" in a class name. Then the message tells you already: "constants must have a value assigned" , which you do not : public const string CONSTANT_VAR; - It has to be assigned right away. If you cannot do that, you cannot use a const. What you can use instead is a public readonly field. That can be assigned in ctor.
    – Fildor
    Commented Jun 14, 2021 at 15:24
  • After someone changes a and/or b (like in your Main method) should CONSTANT_VAR reflect that change or stay as it was when the obj instance was constructed?
    – user10608418
    Commented Jun 14, 2021 at 15:28

2 Answers 2

7

You're looking for read-only fields or properties, not const which is for genuine global constants.

I'd recommend avoiding public fields entirely, and instead using properties - so in this case you'd want a get-only property. Following .NET naming conventions, you'd have something like:

public class Obj
{
    public int A { get; set; }
    public int B { get; set; }

    public string ConstantVar { get; }

    public Obj(int x, int y)
    {
        A = x;
        B = y;
        ConstantVar = /* complex expression */
    }

    public int Do() => A + B - ConstantVar.Length;
}
5
  • 4
    OP: Mind that Obj is still a pretty poor choice for a class name.
    – Fildor
    Commented Jun 14, 2021 at 15:27
  • 4
    @Fildor: Absolutely. And Do is still a very poor choice for a method name, too.
    – Jon Skeet
    Commented Jun 14, 2021 at 15:28
  • Unrelated: Mind that in this examples A and B have setters, which means that you probably need to recalculate ConstantVar when they change, too.
    – Fildor
    Commented Jun 14, 2021 at 15:30
  • 1
    @Fildor: I was going by the fact that a and b in the original code can change, but it sounds like the OP wants ConstantVar to stay the same. Oh the joy of ambiguous questions :(
    – Jon Skeet
    Commented Jun 14, 2021 at 15:33
  • Fair enough, I think it should be good enough to give OP the basic idea. Just wanted to point it out explicitly.
    – Fildor
    Commented Jun 14, 2021 at 15:34
2

You can use Readonly, it gives you option to set the value once and can not be changed later.

public class obj(){
    public int a, b;
    public readonly 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());
    }
}
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.