[IronPython] Re: Why won't these snippets run?

Martin Maly Martin.Maly at microsoft.com
Mon Sep 19 17:45:14 CEST 2005


Hi Ray,

Fredrik already asnwered your generator expression related question. As
for the arrays,
Here is solution that seems to work:

>>> from System import *
>>> a = Array.CreateInstance(Int32, 10)
>>> a
System.Int32[](0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
>>> r = range(10)
>>> for i in r: a[i] = i
>>> a
System.Int32[](0, 1, 2, 3, 4, 5, 6, 7, 8, 9)

I tried your code snippet, but couldn't identify what the GetType
function was.

I hope this helps. Again, it is not a perfect solution and it is one of
many items on our to-do list to make Python built-in types and .Net
types play better together.

Martin

> -----Original Message-----
> From: users-ironpython.com-bounces at lists.ironpython.com 
> [mailto:users-ironpython.com-bounces at lists.ironpython.com] On 
> Behalf Of Ray Djajadinata
> Sent: Monday, September 19, 2005 12:12 AM
> To: users-ironpython.com at lists.ironpython.com
> Subject: [IronPython] Re: Why won't these snippets run?
> 
> Hi Martin!
> 
> Thanks! I think last time you mentioned that IP already had 
> generators--were you referring to 0.9.2 or some other version 
> in the future?
> 
> Also... regarding the Array, is there any workaround?
> I tried this:
> 
> from System import Array, Convert, Int32 pyList = [9, 7, 8, 
> 4, 6, 3, 6, 8, 4, 1] toBeSorted = Array.CreateInstance(GetType(Int32),
> len(pyList))
> for index, item in enumerate(pyList):
>     toBeSorted.SetValue(item, index)
> Array.Sort(toBeSorted)
> 
> but I got this:
> 
> Traceback (most recent call last):
>    at __main__.Initialize() in
> C:\temp\workaround.py:line 5
> System.InvalidCastException: Object cannot be stored in an 
> array of this type.
> 
> Cheers
> Ray



More information about the Ironpython-users mailing list