Connecting Tech Pros Worldwide Forums | Help | Site Map

loading a dictionaryEntry, Specified cast is not valid.

andrewcw
Guest
 
Posts: n/a
#1: Feb 22 '06
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

Mark R. Dawson
Guest
 
Posts: n/a
#2: Feb 22 '06

re: loading a dictionaryEntry, Specified cast is not valid.


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:
[color=blue]
> 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[/color]
andrewcw
Guest
 
Posts: n/a
#3: Feb 22 '06

re: loading a dictionaryEntry, Specified cast is not valid.


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:
[color=blue]
> 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:
>[color=green]
> > 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[/color][/color]
Closed Thread