Reputation: 672
I need to use Hastable
(Not List
and not Dictionary
), and I have many variables with keys. I add keys and variables to class, and use it in my program. But I don't know how to parse Hashtable
. I tried this:
Hashtable toboofer = null;
string path = @"my.bin";
FileStream fin = File.OpenRead(path);
try
{
BinaryFormatter bf = new BinaryFormatter();
toboofer = (Hashtable)bf.Deserialize(fin);
for (int i = 0; i <= toboofer.Count; i++ )
//foreach (KeyValuePair<string, string> kvp in toboofer)
{
myclass cl = new myclass();
cl.Fio = toboofer[i].ToString();
cl.About = toboofer[i].ToString();
}
}
but I have an error. When I try string item
or cycle for
I have an error too.
Upvotes: 2
Views: 4768
Reputation: 2761
Hashtable
has DictionaryEntry
as collection element
foreach (DictionaryEntry entry in toboofer)
{
// do something
}
Make list of myclass
from hashtable:
var listOfMyClass = toboofer.Cast<DictionaryEntry>().
Select(e => new myclass()
{ Fio = e.Key.ToString(), About = e.Value.ToString() });
Upvotes: 6
Reputation: 176896
try this hashtable make use if DictionaryEntry, where KeyValuePair generic used by generic dictionary .Net 2 (and onwards)
Aslo note that Hashtable doesnt have generic version of it and Each element in hastable represented by DictionaryEntry
foreach (DictionaryEntry entry in hashtable)
{
Console.WriteLine("{0}, {1}", entry.Key, entry.Value);
}
Upvotes: 1