Watch, Follow, &
Connect with Us
Public Report
Report From: Delphi Prism/Compiler    [ Add a report in this area ]  
Report #:  93944   Status: Open
IDictonaryDicitionaryEntry Doesn't Work with Generic Dictionaries
Project:  Embarcadero Prism Build #:  4.0.27.843
Version:    14.1 Submitted By:   Bill Fruit
Report Type:  Basic functionality failure Date Reported:  5/16/2011 6:21:07 AM
Severity:    Critical / Show Stopper Last Updated: 3/20/2012 2:24:39 AM
Platform:    All versions Internal Tracking #:   283226
Resolution: None (Resolution Comments) Resolved in Build: : None
Duplicate of:  None
Voting and Rating
Overall Rating: No Ratings Yet
0.00 out of 5
Total Votes: None
Description
I'm trying to iterate through a generic dictionary cast as an IDictionary (which generic dictionaries implement) using DictionaryEntry.  When I do so, I get an invalid cast exception.  Same code works fine in C#

Carlo has code.

Added by Sysop
<<<<<
Please see comments of QC for more details.
>>>>>
Steps to Reproduce:
Create a console program and enter this code in Main:

var dic := new Dictionary<String, Integer>;
dic['a'] := 1;
dic['b'] := 2;
var idic := IDictionary(dic);
for each entry: DictionaryEntry in idic do //<<-- exception here!
begin
  Console.Writeline(entry.Key.ToString, entry.Value.ToString);
end;
Workarounds
None
Attachment
None
Comments

Tomohiro Takahashi at 5/16/2011 7:28:55 AM -
If you use 'for each' loop, please use IEnumerable type and KeyValuePair type as below
  --------
  ...
  var idic := IEnumerable(dic);
  for each entry: KeyValuePair<String, Integer> in idic do
  ...
  --------
instead of
  --------
  ...
  var idic := IDictionary(dic);
  for each entry: DictionaryEntry in idic do
  ...
  --------

Bill Fruit at 5/17/2011 12:22:48 AM -
Yes, using KeyValuePair would work.  But the original problem is that it should work starting from an IDictionary.  The problem was found in some Json Serilailzation code which looked at objects to be serialized and found that an object implemented IDictionary (which hashtable and all the other dicitionaries implement, including the generic dictionaries).  For the objects which are IDictionaries, the code would iterate through the IDictionary using DictionaryEntry and serialize the DictionaryEntryies.  I need code that works for ANY IDictionary, including all the generic dictionaries.  The code here is just an example of how a generic dictionary, which is an IDictionary, CANNOT be iterated using DictionaryEntry -- but it should be able to be iterated using DictionaryEntry, like all the other dictionaries that implement IDictionary.  In the real code, I don't know what kind of Dictionary I have -- I just know that it implements IDictionary.  I don't even know if it is a generic dictionary and I certainly don't know what kind of KeyValue pair I can iterate with.  All I know is that it is an IDictionary.  That is why the test code casts the dictionary to an IDictionary because in the real use case, that is all I know -- I need to iterate through an IDictionary and the real use case fails for generic dictionaries -- this is just an example of one of those failures.

If you write my test code in C# and compile, it works just fine with no exception.

Tomohiro Takahashi at 5/17/2011 9:33:55 PM -
According to Prism docwiki,
http://prismwiki.codegear.com/en/For_(keyword)
says that
-------
For Each
'For each' can be used to iterate over anything that implements the IEnumerable interface. 'For each' uses this syntax:
...
-------

But, anyway, I opened this report.

Server Response from: ETNACODE01