Bitterblue
Bitterblue

Reputation: 14075

C# - Mark Variable As Const (Readonly)

Some of my global variables need to be initiated only once. I do it by loading a file and setting them to whatever. Now I want when I try to set a new value to this variable that an exception is thrown.

public class Foo
{
    public static int MIN;

    private static loadConstants()
    {
        MIN = 18;
    }

    public static void Main()
    {
        loadConstants();
        MIN = 15; // this must throw an exception
        // edit: at least mustn't set the new value
    }
}

How can I do that ?

(probably very easy and I'm sorry)

Upvotes: 1

Views: 470

Answers (4)

Imi
Imi

Reputation: 1579

If you can't or don't want to use the static-constructor from the other answers (for example because you have lots of things to do with the type before actually initializing the variables or because you realize that static constructors are a real pain to debug..) you could to other things:


One compile-time solution is to pack the variables in your own type as non-static readonly and hold a static reference to this type

public class Constants
{
    public readonly int MIN;
    public Constants() { MIN = 18; }
}
public class Foo
{
    public static Constants GlobalConstants { get; private set; }

    public static void Main()
    {
        // do lots of stuff
        GlobalConstants = new GlobalConstants();
    }
}

Or you can make the constant into a property, only providing the getter for anyone outside your class. Note, that the declaring class will still be able to change the property.

public class Foo
{
    public static int MIN { get; private set; } }

    public static void Main()
    {
        MIN = 18;
        MIN = 23; // this will still work :(
    }
}

Or - if for some strange reason - you really want an exception instead of a compile error, you can make a property out of the constant and throw your exception in the setter.

public class Foo
{
    static int _min;
    public static int MIN { get { return _min; } set { throw new NotSupportedException(); } }

    public static void Main()
    {
        _min = 18;
    }
}

Upvotes: 2

Bill Gregg
Bill Gregg

Reputation: 7147

Rather than have a public member variable, you could create a public property, and then manage your CONST logic in your implementation.

 private static int? _min;

 public static int MIN
 {
    set { 
            if (!_min.HasValue())
            {
                _min = value;
            }
            else
            {
               throw;
            }
    }

    get {
           return _min.ValueOrDefault();
    }

 }

Upvotes: 0

sim1
sim1

Reputation: 720

public class Foo
{
    public readonly static int MIN;

    static Foo()
    {
        MIN = 18;
    }

    public static void Main()
    {
    }
}

Upvotes: 3

SamiHuutoniemi
SamiHuutoniemi

Reputation: 1595

Create a static constructor, and mark the variable readonly. Then set the value in the constructor.

public static class Foo
{
    public static readonly int MIN;

    static Foo()
    {
        MIN = 18;
    }

    public static void Main()
    {

    }
}

Upvotes: 6

Related Questions