473,395 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 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 5477
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Tao | last post by:
I just upgraded .NET framework to 1.1 and VS.Net to 2003 version and tried to test it out. I created an ASP.NET project using the wizard and tried to run it by hitting "F5". I got an exception:...
4
by: Tyro | last post by:
Can someone shed some light on my error here? Thanks! Specified cast is not valid. Exception Details: System.InvalidCastException: Specified cast is not valid. Source Error: Stack Trace:
3
by: PK9 | last post by:
I am looking for assistance in pinpointing the cause of the following exception. I am getting a "Specified Cast is not valid" exception on my page. I am trying to populate a datagrid. One of my...
2
by: Fabian | last post by:
Hi, I work with asp.net 2.0 and I have a intermittent error, only happens a few times a day. In the page I evaluate a Query String and then I get data form a database. The code snipped: ...
3
by: VB Programmer | last post by:
I am setting up forms authentication. In my code I keep getting this error. Any ideas? Error.... Server Error in '/LandOLots' Application....
0
by: QA | last post by:
I am using a Business Scorecard Accelarator in a Sharepoint Portal 2003 using SQL Server 2005 I am getting the following error: Error,5/7/2005 10:50:14 AM,580,AUE1\Administrator,"Specified cast is...
0
by: Alan Z. Scharf | last post by:
this question in datagrid group for several days with no repsonse. I'm hoping for an answer her because of greater activity in this group. No cross-posting intended. Thanks....
3
by: =?Utf-8?B?UGF1bCBQcmV3ZXR0?= | last post by:
I'm attempting to use LINQ to insert a record into a child table and I'm receiving a "Specified cast is not valid" error that has something to do w/ the keys involved. The stack trace is: ...
2
by: vinrin | last post by:
Thank for your answer. :-) call CheckEmptyNode (treeview) public void CheckEmptyNode( Object N ) { Microsoft.Web.UI.WebControls.TreeNode menuNode = null; ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.