Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / September 2007

Tip: Looking for answers? Try searching our database.

default construct values for hashtables

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
hiddenhippo - 11 Sep 2007 13:06 GMT
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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.