views:

139

answers:

5

Hello, i search for a good solution for mapping data in c#.
At first i have a Character "a" and a angle "0.0" degree.
What is the best solution for the Mapping ? A list ?

One requirement is that i must search for the degree if it not in the "list" then i add a new one.. and so on

thanks for help :)

EDIT: I must find out if the angle exists ! If the angle not exists then add a new char

+4  A: 

Dictionary< double,char>

Example:

Dictionary< double, char> dic = new Dictionary< double, char>();
//Adding a new item
void AddItem(char c, double angle)
{
    if (!dic.ContainsKey(angle))
        dic.Add(angle,c);
}
//Retreiving an item
char GetItem(double angle)
 {
    char c;
    if (!dic.TryGetValue(angle, out c))
        return '';
    else
        return c;   
 }
AlexDrenea
but char, double must be double, char
subprime
done! .
AlexDrenea
how can i get the char for specific double ? and u must fix: void AddItem(double angle, char c, Dictionary<double, char> dic)
subprime
I assume that you use this code inside a class and dic is a member of that class so you don't need to reference it in every function.
AlexDrenea
ah ok sorry ;) i tried only the function :/
subprime
+1  A: 

Use dictionary.

var d =new Dictionary<string,double> ()`
Jenea
A: 
Aamir
u can add a example ?
subprime
after that i can search for an character to specific angle ? thx
subprime
yes, you can do: char ch = ht[angle];
Aamir
your answer is correct in general but for this specific problem where the user wants a "char" key, the hashtable introduces unnedeed overhead and space requirements because it always hashes the key.
AlexDrenea
In .NET 2.0 and above you should really be using Dictionary(TKey, TValue)- see here: http://msdn.microsoft.com/en-us/library/xfhwa508.aspx
RichardOD
u mean:Dictionary< double, char > dic = new Dictionary< double , char>();...void AddItem(double angle, char c){ if (!dic.ContainsKey(angle)) dic.Add(angle, c);}but how can i get the c to angle ?
subprime
yes, you edited your question afterwards
AlexDrenea
+1  A: 

Dictionary should be fine:

Dictionary<string, float> dict = new Dictionary<string, float>();
dict.Add("a", 0.0);
float angle = dict["a"]
if( !dict.Contains("b"))
{
  dict["b"] = 1.0;
}
Marqus
In the question he wants to search by angles, not characters, so angle should be the key.
axk
A: 

Maybe a SortedDictionary...

 private SortedDictionary<string, double> _myStuff;

...

if (!_myStuff.ContainsValue(0))
...
ozczecho