loading a dictionaryEntry, Specified cast is not valid.

G

Guest

Typically I get a DictionaryEntry from a foreach when walking a Hashtable.
My impression is that the foreach returns one item of the hash.

One of my commonly used functions received takes a DictionaryEntry as a
parameter.

In several instances I dont want to recreate a foreach item.
but this code to create the dictionaryEntry did not work: eg:

Hashtable hTableProcedureTables = new Hashtable();
hTableProcedureTables.Add("F","cheese");

DictionaryEntry dObj = new DictionaryEntry();
dObj = (DictionaryEntry)hTableProcedureTables["F"];
// FAILS...Specified cast is not valid.

Why ? & what should I do ?
 
G

Guest

Hi Andrew,
in the code sample you gave you are adding a string type to the hashtable
as the value, then when you try to retrieve it you are trying to cast the
string to a DictionaryEntry object, which is not possible.

Maybe you were looking for something more like:

Hashtable hTableProcedureTables = new Hashtable();
DictionaryEntry dObj = new DictionaryEntry("cheese");
hTableProcedureTables.Add("F", dObj);

DictionaryEntry dObjRetrieved = (DictionaryEntry)hTableProcedureTables["F"];


Hope that helps
Mark Dawson
 
G

Guest

That was very close - I did not realize I needed to provide both the key and
the value of the hash to create the object-

eg I knew the HaskKey and its value so to create the dictionaryEntry I DO
THIS

Hashtable hTableProcedureTables = new Hashtable();
hTableProcedureTables.Add("A", "foo"); // key, value
DictionaryEntry dObj = new DictionaryEntry("A", hTableProcedureTables["A"]);
// adding key, then value.

--
Andrew


Mark R. Dawson said:
Hi Andrew,
in the code sample you gave you are adding a string type to the hashtable
as the value, then when you try to retrieve it you are trying to cast the
string to a DictionaryEntry object, which is not possible.

Maybe you were looking for something more like:

Hashtable hTableProcedureTables = new Hashtable();
DictionaryEntry dObj = new DictionaryEntry("cheese");
hTableProcedureTables.Add("F", dObj);

DictionaryEntry dObjRetrieved = (DictionaryEntry)hTableProcedureTables["F"];


Hope that helps
Mark Dawson
--
http://www.markdawson.org


andrewcw said:
Typically I get a DictionaryEntry from a foreach when walking a Hashtable.
My impression is that the foreach returns one item of the hash.

One of my commonly used functions received takes a DictionaryEntry as a
parameter.

In several instances I dont want to recreate a foreach item.
but this code to create the dictionaryEntry did not work: eg:

Hashtable hTableProcedureTables = new Hashtable();
hTableProcedureTables.Add("F","cheese");

DictionaryEntry dObj = new DictionaryEntry();
dObj = (DictionaryEntry)hTableProcedureTables["F"];
// FAILS...Specified cast is not valid.

Why ? & what should I do ?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top