[Mono-list] Problem with foreach on a SortedList

Philippe Lavoie philippe.lavoie@cactus.ca
Thu, 15 May 2003 09:31:38 -0400


This is a multi-part message in MIME format.

------_=_NextPart_001_01C31AE6.501C89E0
Content-Type: multipart/alternative;
	boundary="----_=_NextPart_002_01C31AE6.501C89E0"


------_=_NextPart_002_01C31AE6.501C89E0
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

I wrote a unit test for the problem.

=20

If you look at the unit test, you'll notice  that the SortedList =
iterator should return a DictionaryEntry and not the value of the entry. =
This might be the problem. The direct access method returns the value =
and not a DictionaryEntry. See the unit test for a comparison.

=20

=20

Philippe Lavoie

=20

   Cactus Commerce           Software Developer  *  D=E9veloppeur de =
logiciels
eBusiness. All Business.          philippe.lavoie@cactuscommerce.com

 =20

-----Original Message-----
From: POUSSINEAU Herv=E9 [mailto:herve.poussineau@cegetel.fr]=20
Sent: Thursday, May 15, 2003 7:21 AM
To: 'mono-list@lists.ximian.com'
Subject: [Mono-list] Problem with foreach on a SortedList

=20

Hello,=20

First of all, I know that it is not the right place to send bug reports, =
but=20
I can't access Bugzilla at work...=20

I have a problem when executing this code with mono 0.24:=20
using System.Collections;=20
class Problem=20
{=20
   public static void Main(string[] args)=20
   {=20
      SortedList list =3D new SortedList();=20
      list.Add(list.Count, new Queue());=20
      list.Add(list.Count, new Hashtable());=20
      list.Add(list.Count, new Stack());=20
      foreach (DictionaryEntry de in list)=20
      {=20
         System.Console.WriteLine("Item #{0} =3D {1}", de.Key,=20
de.Value.GetType().FullName);=20
      }=20
   }=20
}=20
This code compiles fine with mcs 0.24 and csc 1.0=20

When executed with ms runtime (1.0), results are ok:=20
Item #0 =3D System.Collections.Queue=20
Item #1 =3D System.Collections.Hashtable=20
Item #2 =3D System.Collections.Stack=20

When executed with mono, a runtime error occurs (at the foreach line):=20
Unhandled Exception: System.InvalidCastException: Cannot cast from =
source=20
type to destination type=20
in <0x00207> 00 .Problem:Main (string[])=20

What am I doing wrong?=20

Thanks a lot for your porting of .Net technology to *nix systems=20

Herv=E9=20


------_=_NextPart_002_01C31AE6.501C89E0
Content-Type: text/html;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html>

<head>
<meta http-equiv=3DContent-Type content=3D"text/html; =
charset=3Diso-8859-1">
<meta name=3DGenerator content=3D"Microsoft Word 10 (filtered)">
<title>Problem with foreach on a SortedList</title>

<style>
<!--
 /* Font Definitions */
 @font-face
	{font-family:Tahoma;
	panose-1:2 11 6 4 3 5 4 4 2 4;}
 /* Style Definitions */
 p.MsoNormal, li.MsoNormal, div.MsoNormal
	{margin:0in;
	margin-bottom:.0001pt;
	font-size:12.0pt;
	font-family:"Times New Roman";}
a:link, span.MsoHyperlink
	{color:blue;
	text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
	{color:purple;
	text-decoration:underline;}
p
	{margin-right:0in;
	margin-left:0in;
	font-size:12.0pt;
	font-family:"Times New Roman";}
span.EmailStyle18
	{font-family:Arial;
	color:navy;}
@page Section1
	{size:8.5in 11.0in;
	margin:1.0in 1.25in 1.0in 1.25in;}
div.Section1
	{page:Section1;}
-->
</style>

</head>

<body lang=3DEN-US link=3Dblue vlink=3Dpurple>

<div class=3DSection1>

<p class=3DMsoNormal><font size=3D2 color=3Dnavy face=3DArial><span =
style=3D'font-size:
10.0pt;font-family:Arial;color:navy'>I wrote a unit test for the =
problem.</span></font></p>

<p class=3DMsoNormal><font size=3D2 color=3Dnavy face=3DArial><span =
style=3D'font-size:
10.0pt;font-family:Arial;color:navy'>&nbsp;</span></font></p>

<p class=3DMsoNormal><font size=3D2 color=3Dnavy face=3DArial><span =
style=3D'font-size:
10.0pt;font-family:Arial;color:navy'>If you look at the unit test, =
you&#8217;ll
notice=A0 that the SortedList iterator should return a DictionaryEntry =
and not
the value of the entry. This might be the problem. The direct access =
method
returns the value and not a DictionaryEntry. See the unit test for a
comparison.</span></font></p>

<p class=3DMsoNormal><font size=3D2 color=3Dnavy face=3DArial><span =
style=3D'font-size:
10.0pt;font-family:Arial;color:navy'>&nbsp;</span></font></p>

<p class=3DMsoNormal><font size=3D2 color=3Dnavy face=3DArial><span =
style=3D'font-size:
10.0pt;font-family:Arial;color:navy'>&nbsp;</span></font></p>

<div>

<p class=3DMsoNormal><font size=3D2 color=3Dnavy face=3DArial><span =
style=3D'font-size:
 10.0pt;font-family:Arial;color:navy'>Philippe Lavoie</span></font></p>

<div>

<p class=3DMsoNormal><font size=3D3 color=3Dnavy face=3D"Times New =
Roman"><span
style=3D'font-size:12.0pt;color:navy'>&nbsp;</span></font></p>

</div>

<p class=3DMsoNormal><font size=3D2 color=3Dnavy face=3DArial><span =
style=3D'font-size:
10.0pt;font-family:Arial;color:navy'>&nbsp;&nbsp; Cactus
Commerce&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
Software
Developer&nbsp; &#8226;&nbsp; D=E9veloppeur de logiciels<br>
eBusiness. All =
Business.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a
href=3D"mailto:philippe.lavoie@cactuscommerce.com">philippe.lavoie@cactus=
commerce.com</a></span></font></p>

<p class=3DMsoNormal><font size=3D3 color=3Dnavy face=3D"Times New =
Roman"><span
style=3D'font-size:12.0pt;color:navy'>&nbsp;&nbsp;</span></font></p>

</div>

<p class=3DMsoNormal style=3D'margin-left:.5in'><font size=3D2 =
face=3DTahoma><span
style=3D'font-size:10.0pt;font-family:Tahoma'>-----Original =
Message-----<br>
<b><span style=3D'font-weight:bold'>From:</span></b> POUSSINEAU Herv=E9
[mailto:herve.poussineau@cegetel.fr] <br>
<b><span style=3D'font-weight:bold'>Sent:</span></b> </span></font><font =
size=3D2 face=3DTahoma><span =
style=3D'font-size:10.0pt;font-family:Tahoma'>Thursday, May
 15, 2003</span></font><font size=3D2 face=3DTahoma><span =
style=3D'font-size:10.0pt;
font-family:Tahoma'> </span></font><font size=3D2 face=3DTahoma><span
 style=3D'font-size:10.0pt;font-family:Tahoma'>7:21 =
AM</span></font><font size=3D2
face=3DTahoma><span style=3D'font-size:10.0pt;font-family:Tahoma'><br>
<b><span style=3D'font-weight:bold'>To:</span></b> '</span></font><font =
size=3D2
  face=3DTahoma><span =
style=3D'font-size:10.0pt;font-family:Tahoma'>mono-list</span></font><fon=
t
 size=3D2 face=3DTahoma><span =
style=3D'font-size:10.0pt;font-family:Tahoma'>@lists.ximian.com</span></f=
ont><font
size=3D2 face=3DTahoma><span =
style=3D'font-size:10.0pt;font-family:Tahoma'>'<br>
<b><span style=3D'font-weight:bold'>Subject:</span></b> [Mono-list] =
Problem with
foreach on a SortedList</span></font></p>

<p class=3DMsoNormal style=3D'margin-left:.5in'><font size=3D3 =
face=3D"Times New Roman"><span
style=3D'font-size:12.0pt'>&nbsp;</span></font></p>

<p style=3D'margin-left:.5in'><font size=3D2 face=3D"Times New =
Roman"><span
style=3D'font-size:10.0pt'>Hello,</span></font> </p>

<p style=3D'margin-left:.5in'><font size=3D2 face=3D"Times New =
Roman"><span
style=3D'font-size:10.0pt'>First of all, I know that it is not the right =
place to
send bug reports, but</span></font> <br>
<font size=3D2><span style=3D'font-size:10.0pt'>I can't access Bugzilla =
at work...</span></font>
</p>

<p style=3D'margin-left:.5in'><font size=3D2 face=3D"Times New =
Roman"><span
style=3D'font-size:10.0pt'>I have a problem when executing this code =
with mono
0.24:</span></font> <br>
<font size=3D2><span style=3D'font-size:10.0pt'>using =
System.Collections;</span></font>
<br>
<font size=3D2><span style=3D'font-size:10.0pt'>class =
Problem</span></font> <br>
<font size=3D2><span style=3D'font-size:10.0pt'>{</span></font> <br>
<font size=3D2><span style=3D'font-size:10.0pt'>&nbsp;&nbsp; public =
static void
Main(string[] args)</span></font> <br>
<font size=3D2><span style=3D'font-size:10.0pt'>&nbsp;&nbsp; =
{</span></font> <br>
<font size=3D2><span =
style=3D'font-size:10.0pt'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
SortedList list =3D new SortedList();</span></font> <br>
<font size=3D2><span =
style=3D'font-size:10.0pt'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
list.Add(list.Count, new Queue());</span></font> <br>
<font size=3D2><span =
style=3D'font-size:10.0pt'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
list.Add(list.Count, new Hashtable());</span></font> <br>
<font size=3D2><span =
style=3D'font-size:10.0pt'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
list.Add(list.Count,
new Stack());</span></font> <br>
<font size=3D2><span =
style=3D'font-size:10.0pt'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
foreach (DictionaryEntry de in list)</span></font> <br>
<font size=3D2><span =
style=3D'font-size:10.0pt'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
{</span></font>
<br>
<font size=3D2><span =
style=3D'font-size:10.0pt'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs=
p;
System.Console.WriteLine(&quot;Item #{0} =3D {1}&quot;, =
de.Key,</span></font> <br>
<font size=3D2><span =
style=3D'font-size:10.0pt'>de.Value.GetType().FullName);</span></font>
<br>
<font size=3D2><span =
style=3D'font-size:10.0pt'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
}</span></font>
<br>
<font size=3D2><span style=3D'font-size:10.0pt'>&nbsp;&nbsp; =
}</span></font> <br>
<font size=3D2><span style=3D'font-size:10.0pt'>}</span></font> <br>
<font size=3D2><span style=3D'font-size:10.0pt'>This code compiles fine =
with mcs
0.24 and csc 1.0</span></font> </p>

<p style=3D'margin-left:.5in'><font size=3D2 face=3D"Times New =
Roman"><span
style=3D'font-size:10.0pt'>When executed with ms runtime (1.0), results =
are ok:</span></font>
<br>
<font size=3D2><span style=3D'font-size:10.0pt'>Item #0 =3D =
System.Collections.Queue</span></font>
<br>
<font size=3D2><span style=3D'font-size:10.0pt'>Item #1 =3D
System.Collections.Hashtable</span></font> <br>
<font size=3D2><span style=3D'font-size:10.0pt'>Item #2 =3D =
System.Collections.Stack</span></font>
</p>

<p style=3D'margin-left:.5in'><font size=3D2 face=3D"Times New =
Roman"><span
style=3D'font-size:10.0pt'>When executed with mono, a runtime error =
occurs (at
the foreach line):</span></font> <br>
<font size=3D2><span style=3D'font-size:10.0pt'>Unhandled Exception: =
System.InvalidCastException:
Cannot cast from source</span></font> <br>
<font size=3D2><span style=3D'font-size:10.0pt'>type to destination =
type</span></font>
<br>
<font size=3D2><span style=3D'font-size:10.0pt'>in &lt;0x00207&gt; 00 =
.Problem:Main
(string[])</span></font> </p>

<p style=3D'margin-left:.5in'><font size=3D2 face=3D"Times New =
Roman"><span
style=3D'font-size:10.0pt'>What am I doing wrong?</span></font> </p>

<p style=3D'margin-left:.5in'><font size=3D2 face=3D"Times New =
Roman"><span
style=3D'font-size:10.0pt'>Thanks a lot for your porting of .Net =
technology to
*nix systems</span></font> </p>

<p style=3D'margin-left:.5in'><font size=3D2 face=3D"Times New =
Roman"><span
style=3D'font-size:10.0pt'>Herv=E9</span></font> </p>

</div>

</body>

</html>
=00
------_=_NextPart_002_01C31AE6.501C89E0--

------_=_NextPart_001_01C31AE6.501C89E0
Content-Type: application/octet-stream;
	name="ForEachCast.cs"
Content-Transfer-Encoding: base64
Content-Description: ForEachCast.cs
Content-Disposition: attachment;
	filename="ForEachCast.cs"

dXNpbmcgU3lzdGVtOw0KdXNpbmcgU3lzdGVtLkNvbGxlY3Rpb25zOw0KdXNpbmcgTlVuaXQuRnJh
bWV3b3JrOw0KDQpuYW1lc3BhY2UgTnVuaXRUZXN0cw0Kew0KCS8vLyA8c3VtbWFyeT4NCgkvLy8g
Rm9yZWFjaCBvbiBhIFNvcnRlZExpc3Qgc2hvdWxkIHJldHVybiBhIERpY3Rpb25hcnlFbnRyeQ0K
CS8vLyA8L3N1bW1hcnk+DQoJW1Rlc3RGaXh0dXJlXQ0KCXB1YmxpYyBjbGFzcyBG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