Declare a dictionary in a static class

How to declare a static dictionary object in a static class? I tried

public static class ErrorCode { public const IDictionary<string, string> ErrorCodeDic = new Dictionary<string, string>() { { "1", "User name or password problem" } }; } 

But the compiler complains that a "const" field of a reference type other than a string can only be initialized to zero. "

+73
dictionary c #
Nov 24 '08 at 3:07
source share
9 answers

If you want to declare a dictionary once and never change it, declare it as readonly:

 private static readonly Dictionary<string, string> ErrorCodes = new Dictionary<string, string> { { "1", "Error One" }, { "2", "Error Two" } }; 

If you want the dictionary entries to be read-only (not just links, but also the elements in the collection), you will need to create a read-only dictionary class that implements IDictionary.

Check ReadOnlyCollection for reference.

BTW const can only be used when declaring scalar inline values.

+169
Nov 24 '08 at 3:30
source share

The correct syntax (as verified in VS 2008 SP1):

 public static class ErrorCode { public static IDictionary<string, string> ErrorCodeDic; static ErrorCode() { ErrorCodeDic = new Dictionary<string, string>() { {"1", "User name or password problem"} }; } } 
+7
Nov 24 '08 at 3:23
source share

Old question, but I found this helpful. It turns out that there is also a specialized class for a dictionary using a string for both a key and a value:

 private static readonly StringDictionary SegmentSyntaxErrorCodes = new StringDictionary { { "1", "Unrecognized segment ID" }, { "2", "Unexpected segment" } }; 

Edit: Comment from Chris user below, using Dictionary<string, string> over StringDictionary usually preferable, but will depend on your situation. If you are dealing with an older code base, you can restrict it to StringDictionary . Also note that the following line:

 myDict["foo"] 

returns null if myDict is a StringDictionary , but an exception will be thrown in the case of Dictionary<string, string> . See the SO post he mentioned for more information that is the source of this change.

+4
Mar 29 '11 at 15:08
source share

The problem with your original example was primarily with using const , not static ; you cannot create a non-zero reference to a constant in C #.

I believe this would also work:

 public static class ErrorCode { public static IDictionary<string, string> ErrorCodeDic = new Dictionary<string, string>() { {"1", "User name or password problem"} }; } 

Also, as Y Low points out, adding readonly also a good idea, and none of the modifiers discussed here will allow you to modify the dictionary.

+1
Nov 24 '08 at 3:38
source share

Create a static constructor to add values ​​to the dictionary

 enum Commands { StudentDetail } public static class Quires { public static Dictionary<Commands, String> quire = new Dictionary<Commands, String>(); static Quires() { quire.add(Commands.StudentDetail,@"SELECT * FROM student_b"); } } 
+1
Apr 18 '13 at 8:29
source share

Make the dictionary static and never add it outside of your static ctor object. This seems to be a simpler solution than messing with static / const rules in C #.

0
Nov 24 '08 at 3:21
source share

OK - so I work in ASP 2.x (not my choice ... but hey, who is the bitch?).

None of the dictionary initialization examples will work. Then I came across this: http://kozmic.pl/archive/2008/03/13/framework-tips-viii-initializing-dictionaries-and-collections.aspx

... which prompted me to not use collection initialization in ASP 2.x.

0
May 25 '10 at 17:17
source share

You can use the static / class constructor to initialize the dictionary:

 public static class ErrorCode { public const IDictionary<string, string> ErrorCodeDic; public static ErrorCode() { ErrorCodeDic = new Dictionary<string, string>() { {"1", "User name or password problem"} }; } } 
-one
Nov 24 '08 at 3:13
source share
 public static class ErrorCode { public const IDictionary<string , string > m_ErrorCodeDic; public static ErrorCode() { m_ErrorCodeDic = new Dictionary<string, string>() { {"1","User name or password problem"} }; } } 

Probably initialized in the constructor.

-2
Nov 24 '08 at 3:11
source share



All Articles