forums.silverfrost.com Forum Index forums.silverfrost.com
Welcome to the Silverfrost forums
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

C# and Fortran arrays

 
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> Support
View previous topic :: View next topic  
Author Message
j clark



Joined: 29 Mar 2008
Posts: 20
Location: Bala Cynwyd (Pennsylvania)

PostPosted: Thu May 22, 2008 8:14 pm    Post subject: C# and Fortran arrays Reply with quote

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?
Back to top
View user's profile Send private message AIM Address
Andrew



Joined: 09 Sep 2004
Posts: 232
Location: Frankfurt, Germany

PostPosted: Thu May 22, 2008 10:30 pm    Post subject: Reply with quote

Have you tried using System.Int32 or System.Int16?
Back to top
View user's profile Send private message
j clark



Joined: 29 Mar 2008
Posts: 20
Location: Bala Cynwyd (Pennsylvania)

PostPosted: Thu May 22, 2008 11:58 pm    Post subject: Reply with quote

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
Back to top
View user's profile Send private message AIM Address
PaulLaidler
Site Admin


Joined: 21 Feb 2005
Posts: 7924
Location: Salford, UK

PostPosted: Fri May 23, 2008 9:37 am    Post subject: Reply with quote

Try declaring F_ARRAY as CHARACTER. Then use ICHAR.
Back to top
View user's profile Send private message AIM Address
Andrew



Joined: 09 Sep 2004
Posts: 232
Location: Frankfurt, Germany

PostPosted: Fri May 23, 2008 10:26 am    Post subject: Reply with quote

Another possible way: pass an Int32 array from the fortran

Code:

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#:


Code:

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]);
     }
}
Back to top
View user's profile Send private message
j clark



Joined: 29 Mar 2008
Posts: 20
Location: Bala Cynwyd (Pennsylvania)

PostPosted: Fri May 23, 2008 1:40 pm    Post subject: Thank you. Reply with quote

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
Back to top
View user's profile Send private message AIM Address
Display posts from previous:   
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> Support All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group