Silverfrost Forums

Welcome to our forums

C# and Fortran arrays

22 May 2008 7:14 #3239

Hello, I'm back with what should be a simple question.

How do you load or store C# byte[] or int[] arrays into the Fortran equivalent? For example,

OBJECT('System.Byte[]') C_ARRAY C_ARRAY=NEW@('System.Byte[]',256) INTEGER(KIND=1) F_ARRAY(256) ... DO I=1,256 C_ARRAY(I-1) ??? F_ARRAY(I) END DO ...

If I pass F_ARRAY to the C# method, I get 'No .NET Method matches...'; If I try a simple assignment ('='), I get 'You cannot assign...'. What, then?

22 May 2008 9:30 #3241

Have you tried using System.Int32 or System.Int16?

22 May 2008 10:58 #3245

Actually, Andrew,

.Byte[] is what is essential since I am using .Net.Sockets methods to communicate with the remote systems and .Byte[] is the only overload to those methods.

But thanks,

Jerry

23 May 2008 8:37 #3246

Try declaring F_ARRAY as CHARACTER. Then use ICHAR.

23 May 2008 9:26 #3248

Another possible way: pass an Int32 array from the fortran

INTEGER F_ARRAY(256)
OBJECT('ClassLibrary3.Class1') :: OBJ
ASSEMBLY_EXTERNAL(name='ClassLibrary3.Class1.TestRoutine') :: TESTROUTINE
OBJECT('System.Int32[]') C_ARRAY

OBJ = new@('ClassLibrary3.Class1')

C_ARRAY=NEW@('System.Int32[]',256)

! make sure the indexing is correct
DO I=0,255
    F_ARRAY(I + 1) = I
    C_ARRAY(I) = F_ARRAY(I + 1)
END DO 


CALL TESTROUTINE(OBJ, C_ARRAY)
END PROGRAM

And then do the conversion to byte[] in the C#:

public void TestRoutine(int[] arr)
{
     byte[] ba = new byte[arr.Length];
     for (int i = 0; i < arr.Length; i++)
     {
         ba[i] = Convert.ToByte(arr[i]);
     }
}
23 May 2008 12:40 #3250

Thank you, Andrew, once again. Works perfectly, with 16-bit integers, to boot. I'm guessing the the compiler (correctly) objects to assigning an 8-bit integer to an unsigned byte. (Sbyte[] would probably work, but that defeats the purpose.) And Paul, as a matter of fact I had been passing CHAR strings to C# strings for testing communications, etc. but a couple of days ago I found that I was dropping the 8th bit when converting to a C# byte[] array and I can find no method in the .Convert or .Encoding classes that work. But also, thanks.

Jerry

Please login to reply.