2

I am trying to understand how to pass a multi-dimensional array of float from IronPython code to a C# library.

Here is the C# code I am trying to call (This is a function is a library class I am importing into my IronPython code):

public void ShowMessage(double[,] values)

This is the my IronPython code:

import clr
clr.AddReferenceToFile(r"DisplayLib.dll")
from DisplayLib import Display

display = Display()

a = [[1.2, 1.3, 1.4, 1.5],
     [2.2, 2.3, 2.4, 2.5]]

display.ShowMessage(a)

I am getting the following exception: "expected Array[float], got list" then I tried to convert the array to a tuple but it only worked for a 1D array.

Any suggestions on how do this?

1 Answer 1

5

You'll need to create an instance of a Two-Dimensional .NET array. You cannot use Python lists in place of arrays. An unfortunate limitation.

You could try something like this:

from System import Array

data = [[1.2, 1.3, 1.4, 1.5],
        [2.2, 2.3, 2.4, 2.5]]
# assuming all rows will have the same length
a = Array.CreateInstance(float, len(data), len(data[0]))
for i, row in enumerate(data):
    for j, col in enumerate(row):
        a[i, j] = col
display.ShowMessage(a);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.