locked
Non-String Constant RRS feed

  • Question

  • User-1245426334 posted

    Hey, everyone!

    This is probably a really basic thing to ask, but how do I make a non-root object a constant so it can never be changed?

    Here's my example:

    public const NetworkCredential API_CREDENTIALS = new System.Net.NetworkCredential(username, password);

    That won't compile because "A const field of a reference type other than string can only be initialized with null."

    Well, initializing it to null does me no good because I want it to have a value. I just don't want that value changed.

    Is the only way to make a public property that only has a get-statement?

    Perhaps I'm overthinking this and my brain is shutting down prior to the long weekend. :)

    Thanks in advance for the advice!

    -Eric

    Wednesday, July 2, 2014 12:44 PM

Answers

  • User-1360095595 posted

    Use readonly instead. 

    Refer: http://msdn.microsoft.com/en-us/library/acdd6hb7.aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 2, 2014 12:57 PM
  • User281315223 posted

    If you don't want your value to be altered, you could consider using the readonly keyword instead of a constant (as constants can only be "numbers, Boolean values, strings, or a null reference") : 

    public readonly NetworkCredential API_CREDENTIALS = new System.Net.NetworkCredential(username, password);

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 2, 2014 1:05 PM

All replies

  • User-1360095595 posted

    Use readonly instead. 

    Refer: http://msdn.microsoft.com/en-us/library/acdd6hb7.aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 2, 2014 12:57 PM
  • User281315223 posted

    If you don't want your value to be altered, you could consider using the readonly keyword instead of a constant (as constants can only be "numbers, Boolean values, strings, or a null reference") : 

    public readonly NetworkCredential API_CREDENTIALS = new System.Net.NetworkCredential(username, password);

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 2, 2014 1:05 PM
  • User-1245426334 posted

    Thanks, guys. I thought about readonly, but I wasn't sure about that either.

    Appreciate the feedback!

    Wednesday, July 2, 2014 1:32 PM
  • User397347636 posted

    'readonly' will prevent you from setting it (except for the constructor), but you can always change the state of the object.

    e.g., you can always change the username or password anytime:

    API_CREDENTIALS.UserName = "something else";
    API_CREDENTIALS.Password = "something else";


    This might not be what you intended.  All readonly does is prevent you from later resetting it:

    API_CREDENTIALS = null; //not allowed with 'readonly' after instantiation
    //or:
    API_CREDENTIALS = new System.Net.NetworkCredential(different_username, different_password); //not allowed after initial instantiation

    In general, the idea of a "non-changeable" object does not exist in C# - you have some specialized classes that are readonly, but you can't make an arbitrary type instance "unchangeable".

    Wednesday, July 2, 2014 2:35 PM