471,350 Members | 1,946 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,350 software developers and data experts.

loading a dictionaryEntry, Specified cast is not valid.

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 ?

--
Andrew
Feb 22 '06 #1
2 5332
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" wrote:
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 ?

--
Andrew

Feb 22 '06 #2
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" wrote:
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" wrote:
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 ?

--
Andrew

Feb 22 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Tyro | last post: by
3 posts views Thread by PK9 | last post: by
3 posts views Thread by VB Programmer | last post: by
reply views Thread by Alan Z. Scharf | last post: by
3 posts views Thread by =?Utf-8?B?UGF1bCBQcmV3ZXR0?= | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.