loading a dictionaryEntry, Specified cast is not valid.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • andrewcw

    #1

    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 hTableProcedure Tables = new Hashtable();
    hTableProcedure Tables.Add("F", "cheese");

    DictionaryEntry dObj = new DictionaryEntry ();
    dObj = (DictionaryEntr y)hTableProcedu reTables["F"];
    // FAILS...Specifi ed cast is not valid.

    Why ? & what should I do ?

    --
    Andrew
  • Mark R. Dawson

    #2
    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 hTableProcedure Tables = new Hashtable();
    DictionaryEntry dObj = new DictionaryEntry ("cheese");
    hTableProcedure Tables.Add("F", dObj);

    DictionaryEntry dObjRetrieved = (DictionaryEntr y)hTableProcedu reTables["F"];


    Hope that helps
    Mark Dawson
    --
    Instantly claim a ₹3,888 Welcome Bonus and play the latest online slot games from LOTUS365. Download the Lotus365 App. Our website offers a variety of free activities.



    "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 hTableProcedure Tables = new Hashtable();
    > hTableProcedure Tables.Add("F", "cheese");
    >
    > DictionaryEntry dObj = new DictionaryEntry ();
    > dObj = (DictionaryEntr y)hTableProcedu reTables["F"];
    > // FAILS...Specifi ed cast is not valid.
    >
    > Why ? & what should I do ?
    >
    > --
    > Andrew[/color]

    Comment

    • andrewcw

      #3
      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 hTableProcedure Tables = new Hashtable();
      hTableProcedure Tables.Add("A", "foo"); // key, value
      DictionaryEntry dObj = new DictionaryEntry ("A", hTableProcedure Tables["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 hTableProcedure Tables = new Hashtable();
      > DictionaryEntry dObj = new DictionaryEntry ("cheese");
      > hTableProcedure Tables.Add("F", dObj);
      >
      > DictionaryEntry dObjRetrieved = (DictionaryEntr y)hTableProcedu reTables["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 hTableProcedure Tables = new Hashtable();
      > > hTableProcedure Tables.Add("F", "cheese");
      > >
      > > DictionaryEntry dObj = new DictionaryEntry ();
      > > dObj = (DictionaryEntr y)hTableProcedu reTables["F"];
      > > // FAILS...Specifi ed cast is not valid.
      > >
      > > Why ? & what should I do ?
      > >
      > > --
      > > Andrew[/color][/color]

      Comment

      Working...