Hi,
I have a class which contains all my constant values such as;
public static readonly string[] strStringOps = {"Equals","Not
Equals","Contains"};
The above nicely assigns the values into the string array. However
I'm looking for a way of achieving the same results whilst using a
hashtable; for example
public static hashtable myhash = new hashtable(idictionary)
however I can't find a way of defining all the the key pairs and
passing that into the above e.g.
public static idictionary mydict = {"key","pair","key1","pair1"};
public static hashtable myhash = new hashtable(mydict)
can anyone help, suggest an alternative way, or am I really stuck with
calling
public static idictionary mydict = new hashtable();
myhash.add("foo","bar");
thanks
Alberto Poblacion - 11 Sep 2007 13:25 GMT
> I have a class which contains all my constant values such as;
>
[quoted text clipped - 17 lines]
> public static idictionary mydict = new hashtable();
> myhash.add("foo","bar");
In C# version 2.0, you are stuck with the .Add method, but in C# version
3.0, you can make use of Object and Collection Initializers to initialize a
collection (which in your case should probably be a Dictionay<string,string>
rather than a Hashtable) in the declaration line.
See
http://msdn2.microsoft.com/en-us/library/ms364047(vs.80).aspx#cs3spec_topic5
Chris Shepherd - 11 Sep 2007 13:39 GMT
> Hi,
>
[quoted text clipped - 21 lines]
>
> thanks
There's a KeyValuePair class you might get away with using in an array
and/or wrapping that in a list:
private static KeyValuePair<string,string>[] myKvP = {
new KeyValuePair<string,string>("key1", "value1"),
new KeyValuePair<string,string>("key2", "value2"),
new KeyValuePair<string,string>("key3", "value3"),
new KeyValuePair<string,string>("key4", "value4")};
public static List<KeyValuePair<string, string>> myList = new
List<KeyValuePair<string, string>>(myKvP);
It really depends on how you want to access it though.
Chris.
Göran Andersson - 11 Sep 2007 13:47 GMT
> Hi,
>
[quoted text clipped - 21 lines]
>
> thanks
Add a static constructor to your class and populate the list there.

Signature
Göran Andersson
_____
http://www.guffa.com