1

I'm programming using C#, after programming on C. So I'm using a lot of constants such as "DEFAULT_USER_ID", "REMOTE_ADDRESS" and such...

It seems to me that it's pretty "old fashioned" to use such constants and maybe there is some other more elegant way for using some constant data between objects.

Any ideas on how this could be done elegantly?

Thanks.

Roman
  • 4,443
  • 14
  • 56
  • 81

7 Answers7

4

Using constants for stuff like DEFAULT_USER_ID is still "the way to go" (unless you want it to be configurable, but that's another topic). --> const (C# Reference)

Don't use constants for enumerations (FILE_TYPE_DOC = 1, FILE_TYPE_XLS = 2, ...). This can be done more elegantly in C# with enums:

enum FileType {
   Doc,
   Xls,   // or, optionally, "Xls = 2".
   ...
};

You can also use this for flags (constants combinable by bitwise operators), which is another common use case of constants in C:

[Flags]
enum FontDecoration {
    None = 0,
    Bold = 1,
    Italic = 2,
    Underline = 4
}
Heinzi
  • 167,459
  • 57
  • 363
  • 519
  • didn't know about the "flags" thingy :) Good to know that it's still "the way to go", it means I'm not too old fashioned ;) – Roman Jan 09 '11 at 13:39
2
  1. Your naming convention doesn't fit .net. Use PascalCase instead of SCREAMING_CAPS
  2. Be aware of the binary versioning semantics of constants in .net. You might want to use a static readonly field instead of const sometimes.
  3. Where it's conceptually a good idea use enums instead of several integer constants.
Community
  • 1
  • 1
CodesInChaos
  • 106,488
  • 23
  • 218
  • 262
1

You can use static or readonly properties, for instance, for App class

class MyClass {

public static readonly int myVal=10;

}
simon
  • 1,161
  • 10
  • 27
  • This couldn't be a good idea, cause the const is determined in compile time and readonly members will be initialized in constructor of class, and their usage is different, you can use a const for sth like PI in math, but using a readonly for PI could not be a good idea – Jahan Zinedine Jan 09 '11 at 13:35
  • Is this better? or just another way of doing the same thing? – Roman Jan 09 '11 at 13:37
  • The readonly keyword is different from the const keyword. A const field can only be initialized at the declaration of the field. A readonly field can be initialized either at the declaration or in a constructor. Therefore, readonly fields can have different values depending on the constructor used. Also, while a const field is a compile-time constant, the readonly field can be used for runtime constants http://msdn.microsoft.com/en-us/library and http://msdn.microsoft.com/en-us/library/e6w8fe1b%28v=vs.71%29.aspx/aa645752%28VS.71%29.aspx – Jahan Zinedine Jan 09 '11 at 13:39
  • Sure, readonly and const are different. But the author asks how to do the same thing more elegantly, and in more modern way... As well, you can always use App class, which will be available anytime – simon Jan 09 '11 at 13:44
  • They're also different because constants are compiled *directly into your code*. You should only use things for constants that are guaranteed to *never* change, like the value of pi. Otherwise, use `readonly`. It's not more elegant if it's broken. – Cody Gray - on strike Jan 09 '11 at 15:32
1

Also you may define constants like that

    public static class Defaults
    {
        public const string MyName = "SuperName";
    }

    public  class MyClass
    {
        string s = Defaults.MyName;
    }

In such case you may use class Defaults anywhere in your app

Also you may want to know that there is two ways of defining constant variables in Static readonly vs const

Community
  • 1
  • 1
angularrocks.com
  • 26,767
  • 13
  • 87
  • 104
1

Another alternative is to store constant values in a .config file, that way you do not need to recompile your application to change your values if needed. Depending on how your code is being deployed, it may or may not be appropriate to have your settings exposed as plain text. See this article for a simple example of using a .config file: http://www.developer.com/net/net/article.php/3396111/Using-Application-Configuration-Files-in-NET.htm

wsanville
  • 37,158
  • 8
  • 76
  • 101
0

You can always just use custom Enums so you get Visual Studio Intellisense.

Echilon
  • 10,064
  • 33
  • 131
  • 217
Machinarius
  • 3,637
  • 3
  • 30
  • 53
0

How about config files? you guys don't use config files in C#? DEFAULT_USER_ID and REMOTE_ADDRESS look very much suited to be in a config file

Nylon Smile
  • 8,990
  • 1
  • 25
  • 34