-4

I am partly modifying an application where I will need to set value of the following constant to the value of the environment variable if exists.

What I already have:

private const string BuildPackagePath = @"\\server\build\";

What I would like to do is:

if (Environment.GetEnvironmentVariable("EnvVar") != null)
Set the property value to = Environment.GetEnvironmentVariable("EnvVar")
else
{Keep default/assigned value}

I understand that the left side of an asignment has to be a variable. I will probably have to change the type but was just wondering if anyone could give me an idea so the structure of current code can be kept as is.

Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
ditu
  • 45
  • 1
  • 8
  • 3
    You can´t modify a constants value, that´s why it´s called *constant*. However you can use `readonly` to indicate that the member can be modified only within the constructor. – MakePeaceGreatAgain Aug 31 '17 at 14:21
  • Thank you! I just wanted to check – ditu Aug 31 '17 at 14:24
  • For most intends and purposes `static readonly` will work like `const` *at runtime*. If you need a compile time constant, you can't use it of course. -- so you could have `private static readonly string BuildPackagePath = Environment.GetEnvironmentVariable("EnvVar") ?? @"\\server\build\";` – Corak Aug 31 '17 at 14:24

3 Answers3

2

You can´t modify a constants value, that´s why it´s called constant. However you can use readonly to indicate that the member can be modified only within the constructor:

class MyClass
{
    private readonly string BuildPackagePath;
    public MyClass()
    {
        var value = Environment.GetEnvironmentVariable("EnvVar");
        if(value != null) this.BuildPackagePath = value;
        else this.BuildPackagePath = @"\server\build\";
    }
}

Or even shorter using the null-conitional operation:

this.BuildPackagePath = value ?? @"\server\build\";
MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111
2

consider using a static property without setter

// evaluates Environment Variable on each call
private static string BuildPackagePath 
{
    get { return Environment.GetEnvironmentVariable("EnvVar") ?? @"\server\build\"; }
}

static readonly field will evaluate Environment Variable only once (but not immediately at startup When do static variables get initialized in C#?)

private static readonly string BuildPackagePath = 
        Environment.GetEnvironmentVariable("EnvVar") ?? @"\server\build\";
ASh
  • 34,632
  • 9
  • 60
  • 82
  • 2
    Mind that this is evaluated every time the getter is called ... if in the course of runtime the EnvVar is set, the result could change. – Fildor Aug 31 '17 at 14:32
  • @Fildor, yes. it maybe desired, or not. if not, then `static readonly` field instead of property should work, since it is initialized once. `readonly` instance field will reevaluate EnvironmentVariable for every new object. many options, choose depending on requirements – ASh Aug 31 '17 at 14:37
  • I agree, it *could* be desired. I just wanted to point that behavior out to avoid "surprises". – Fildor Aug 31 '17 at 14:42
1

You can use "readonly" modifier instead of const. Then you can set a value of the field in a constructor of a class. For example:

class SampleClass
{
      public int x;
      // Initialize a readonly field
      public readonly int y = 25;
      public readonly int z;

      public SampleClass()
      {
         // Initialize a readonly instance field
         z = 24;
      }
}