473,320 Members | 1,865 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Help with Dictionary()

HaLo2FrEeEk
404 256MB
I need help getting this to work. I've got a dictionary object that I need to contain 57 items in. Each item is an int/string pair with the int being an identifier to the string. I'm putting the dictionary in a seperate class file so I don't bloat my main program with too many extra lines of code. I don't want to have any extra files for the user to download.

Since I'm doing it in a class file, I'm trying to simply assign the values to the dictionary like this:

Expand|Select|Wrap|Line Numbers
  1. Dictionary<int, string> data = new Dictionary<int, string>()
  2. {
  3.     myInt, myString
  4. };
The problem is, I don't know how to assign the values into the dictionary using that method. I would rather stay away from making a method just to populate the dictionary if I can do it this way.

Also, I'm not completely 100% decided on using a dictionary anyway. I basically want an associative array. In PHP this would be easy, since I can have an associative array, but C# makes it difficult.

Can anyone please help me figure this out? If I do go with a dictionary, how would I provide an integer and return the string it's associated with?

Finally, is a Dictionary or a Hashtable better for something like this?
Jun 9 '10 #1

✓ answered by ryuk

You can't do that.
Instead of that way..
Expand|Select|Wrap|Line Numbers
  1. Dictionary<int, string> data = new Dictionary<int, string>()
  2. {
  3.     { myInt, myString },
  4.     { 1, string.Empty },
  5.    { 2, null },
  6.    { 3, "" }
  7. };
  8.  

4 1237
Plater
7,872 Expert 4TB
There is no constructor that takes a series of data as parameters.
You could make your own class that inherits from Dictionary maybe and have a constructor that does it?
Jun 9 '10 #2
HaLo2FrEeEk
404 256MB
Maybe I'm misunnderstanding the terminology here, but I'm not trying to pass them at parameters, I'm trying to do something similar to a list:

List<int> myList = new List<int>() { 1, 2, 3};

But with a dictionary.

Regardless, I ended up writing a method to add them all, then another method to read the string back into my main program. It works and it's fast. I'd still like to find a more elegant solution though.
Jun 9 '10 #3
Plater
7,872 Expert 4TB
That is an implicit constructor when you do that, its part of the Array class. I don't think that applies to a dictionary, but I could be wrong.
Jun 9 '10 #4
ryuk
1
You can't do that.
Instead of that way..
Expand|Select|Wrap|Line Numbers
  1. Dictionary<int, string> data = new Dictionary<int, string>()
  2. {
  3.     { myInt, myString },
  4.     { 1, string.Empty },
  5.    { 2, null },
  6.    { 3, "" }
  7. };
  8.  
Jun 9 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: ESPNSTI | last post by:
Hi, I'm trying to use a generics dictionary with a key class that implements and needs IComparable<>. However when I attempt to use the dictionary, it doesn't appear to use the IComparable<> to...
2
by: jg | last post by:
I was trying to get custom dictionary class that can store generic or string; So I started with the example given by the visual studio 2005 c# online help for simpledictionay object That seem...
1
by: john wright | last post by:
I have a dictionary oject I created and I want to bind a listbox to it. I am including the code for the dictionary object. Here is the error I am getting: "System.Exception: Complex...
4
by: Bill Woodruff | last post by:
< note : this message was sparked in part by comments by David Browne on a previous thread : "inserting an anonymous method as a value in a generic dictionary ?" : David had shown the use of...
3
by: aking | last post by:
Dear Python people, im a newbie to python and here...so hello! Im trying to iterate through values in a dictionary so i can find the closest value and then extract the key for that...
8
by: Brian L. Troutwine | last post by:
I've got a problem that I can't seem to get my head around and hoped somebody might help me out a bit: I've got a dictionary, A, that is arbitarily large and may contains ints, None and more...
1
hpbutterbeer
by: hpbutterbeer | last post by:
We have a Machine Project and my brain is currently in a clouded state. Sorry, I'm just a beginner in C Programming... Text twist is a windows game whose main objective is to form words out of the...
3
by: JamesB | last post by:
I have a config screen in my app that updates a dictionary<key,value>. I want to store a copy of this before going into the config screen so if the user wants to cancel all their changes I can...
1
by: sachin2 | last post by:
I am using 3 types of dictionaries. 1) Dictionary<string, string > d = new Dictionary<string, string>(); 2) Dictionary<string, List<string>> d = new Dictionary<string, List<string>>(); 3)...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.