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: - Dictionary<int, string> data = new Dictionary<int, string>()
-
{
-
myInt, myString
-
};
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?
You can't do that.
Instead of that way.. -
Dictionary<int, string> data = new Dictionary<int, string>()
-
{
-
{ myInt, myString },
-
{ 1, string.Empty },
-
{ 2, null },
-
{ 3, "" }
-
};
-
4 1237
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?
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.
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.
You can't do that.
Instead of that way.. -
Dictionary<int, string> data = new Dictionary<int, string>()
-
{
-
{ myInt, myString },
-
{ 1, string.Empty },
-
{ 2, null },
-
{ 3, "" }
-
};
-
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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)...
|
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...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
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)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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....
| |