Jun.27.2011
09:57
09:57
What about passing strings in structures or arrays in structures? I am having trouble with this.
Public Structure pointIn3D
Public x As Double
Public y As Double
Public z As Double
End Structure
Fortunately, Fortran has a way to store a structure in the exact same way we have defined here. To do so, we use the keyword "type". The type keyword in Fortran is the way we can store multiple variables wrapped in single peice. Since Doubles in Visual Basic are represented by eight bytes, we must also ensure that every variable is represented the same way. To finalize it, we wrap it all in something called a module. the end result looks like the following:
module test
type Point
double precision x, y, z
end type Point
end module test
So now, we have the exact representation of the Visual Basic code in Fortran. Passing the data to Fortran is now trvial since we have already defined what the strucutre looks like in both languages.
subroutine modX(ThreeDubs)
use test
type (Point) :: ThreeDubs
ThreeDubs%x = 5.5
end subroutine
The above code is a simple routine that accepts a Point as a parameter, changes the x value to 5.5, and returns control to the caller. "use test" specifies that in this method, we will be using the test module that we specified in the code before the method. In the next line, we specify that the input paramter is indeed of the Point type, and we then change the x value to 5.5. A simple method.<StructLayout(LayoutKind.Sequential)> _
Public Class CustomPoint3d
Public x As Double
Public y As Double
Public z As Double
End Class
If we pass an object of type CustomPoint3d to the previously written Fortran routine, we will now find that the object is read correctly. That's all there is to it! Now, to be able to use this method both of these ways in the same Visual Basic project, it's simply a matter of defining the method twice with two different signatures.
<DllImport("passPoint3d.dll")> _
Public Shared Function PrintX(ByRef point As pointIn3D) As Boolean
End Function
<DllImport("passPoint3d.dll")> _
Public Shared Function PrintX(ByRef point As CustomPoint3d) As Boolean
End Function
What about passing strings in structures or arrays in structures? I am having trouble with this.