Save values in Hashtable

4

When I save the value of this Hastable it gives me error.

Follow the code:

Hashtable ht = new Hashtable();
ht.Add("index", index);
ht.Add("tipo", "1");

string tipo = ht["tipo"];
    
asked by anonymous 09.06.2016 / 16:58

2 answers

5

When you return a value from a Hastable you have to convert it to String like this:

string tipo = ht["tipo"].ToString();
    
09.06.2016 / 17:00
5

First, do not use this data structure, it is considered obsolete and should no longer be used by any application. Instead, Dictionary<K, V> . Once this is done, the problem will not occur. If you insist you will have performance problems and errors of this type, which can even be solved (doing conversion, for example), but not worth the effort.

var ht = new Dictionary<string, string>();
ht.Add("index", "0");
ht.Add("tipo", "1");
string tipo = ht["tipo"];

See running on dotNetFidle and on CodingGround .

    
09.06.2016 / 17:14