default construct values for hashtables

H

hiddenhippo

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
 
A

Alberto Poblacion

hiddenhippo said:
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");

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
 
C

Chris Shepherd

hiddenhippo said:
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

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.
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

hiddenhippo said:
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

Add a static constructor to your class and populate the list there.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top