PDA

View Full Version : Passing array of strings between VB.NET and Fortran


chtr
03-28-2004, 07:15 PM
Hi,

I am trying to pass a array of strings from VB.Net to Fortran

VB :
Dim ArrayStrings(2) As String
ArrayStrings(0) = "One"
ArrayStrings(1) = "Two"

In Fortran, my code in the receiving function looks like

use System
type(String), pointer,dimension(:):: VBArrayOfStrings
character*12 FortranString1

FortranString1 = string(VBArrayOfStrings(0))

This works fine, and FortranString1 doess contain what i'm putting in in VB.Net. ("One"). However, I don't manage to get the second string : using FortranString1=string(VbArrayOfStrings(1)) keeps returning the first string. What am I doing wrong ?

Thanks.

tzeis
03-30-2004, 07:26 PM
You are not really giving enough information to figure out what is happening. Could you post an example that I can build and run that demonstrates the problem?

chtr
03-31-2004, 06:07 AM
i've included in this post a ZIp file containing an example of the previously posted problem :

In a VB.Net main, I define a array of strings (Dim inStrg(10) As String), and fill in the three first elements.
(Button_1 click event of Form1, calling Sub test in Module1.

This subroutine calls a method of the Fortran.Net testclass, testString.
in this subroutine testString, inStrg is of type
type(String),pointer,dimension(:).
I now want to convert all the elements of inStrg into a fortran string (Character*10),
using
DO lc1 = 0 , inSize-1
woord = string( inStrg( lc1 ))
ENDDO
, insize being the number of elements in inStrg which have a value.


This works fine for the first string, but even when the counter lc1 increases, string(inStrg(lc1)) always
returns the first string. In the watch window, I can see that inStrg in Fortran does contain the right values.

I also tried (see commented lines) using the system%Enumerator, but my%Enumerator%get_Current stays empty.


Thanks for looking at it,
CT

tzeis
04-01-2004, 05:33 PM
It looks like the compiler doesn't like the pointer attribute for the dummy string argument. If I remove the pointer attribute for the dummy variable "inStrg", the Fortran class correctly recieves the String array.
I suspect this is due to the immutable nature of strings - once a string has been created, it cannot be changed. Changes are made by destroying the string and creating a new one containing the changes. This means that the possibility exists in your Fortran class that the string passed in by reference might not exist anymore when it is passed back. This danger can be averted if the dummy string argument does not have the pointer attribute. I suspect that you will have a similar problem when trying to pass the result string back to VB.NET. You can resolve this problem by passing the result string back as a function result rather than an argument.

tzeis
04-01-2004, 05:35 PM
It looks like the compiler doesn't like the pointer attribute for the dummy string argument. If I remove the pointer attribute for the dummy variable "inStrg", the Fortran class correctly receives the String array.
I suspect this is due to the immutable nature of strings - once a string has been created, it cannot be changed. Changes are made by destroying the string and creating a new one containing the changes. This means that the possibility exists in your Fortran class that the string passed in by reference might not exist anymore when it is passed back. This danger can be averted if the dummy string argument does not have the pointer attribute. I suspect that you will have a similar problem when trying to pass the output string back to VB.NET. You can resolve this problem by passing the output string back as a function result rather than an argument.