HOW to pass array to sub

margret

Well-Known Member
Licensed User
Longtime User
Hello, I need to pass an array to a sub. I have passed variables just fine but the array is giving errors. In the sub I have tried MyVar2, MyVar(), MyVar As String and not Array. I always get an error. I want to do something like:

Code: Copy
Dim MyVar(2,1) As String
MyVar(0,0)="Bill"
MyVar(0,1)="32"
MyVar(1,0)="Jimmy"
MyVar(1,1)="61"

MySub("ID Listing...", MyVar)


Sub MySub (Text as String, MyVar2 as Array)
     For l=0 to MyVar2.Size
          'show list
     Loop
End Sub
 
Last edited:
Still learning, and I think it might be useful to have more than just a solution.

Can somebody please explain why 'the system' does not know the rank of the array? After all it has been amply declared and Dim'ed up front.
The same principle seems odd to me that other declared typed variables have to be repeated when passed to Arrays.
 
Upvote 0
I agree rank should be handled by the compiler.

This syntax would also have a problem in that if you changed the Dim rank you have to modify all subs using it as a parameter even if the sub is only passing the reference to other subs.

bluejay
 
Upvote 0
Can somebody please explain why 'the system' does not know the rank of the array?
The 'system' knows the rank of the array but the 'sub' does not. After all how can the sub predict what data you want to pass to it.
After all it has been amply declared and Dim'ed up front.
The variable was declared but that has no bearing on the subroutine you want to pass it to.
 
Upvote 0
I was referring to the sub call not the sub definition.

At the call the sub is given a real parameter and the compiler already knows the type of a real parameter.

bluejay
 
Upvote 0
oops! Just realised I was reading the wrong post. The syntax is already what I was suggesting.

bluejay
 
Upvote 0
Thank you for the reason agraham
 
Upvote 0
Array

Hi,
i am trying to receive an array from a java library into B4A. What is the format within B4A as i keep getting errors.
 
Upvote 0
I am missing something here. Keep on getting an error with a difference in rank between passing and receiving. Can anyone help with this?

Code: Copy
Sub Button3_Click
  Dim zz(6) As Double
  zz(0) = 0
  zz(1) = 90
  zz(2) = 0
  zz(3) = 30
  zz(4) = 2
  zz(5) = 0
  zz = JHSMATH.Triangle_AAS(zz)

End Sub

'the following code is in a module named JHSMATH

Public Sub Triangle_AAS (aa() As Double) as Double()

  Dim s1 As Double
  Dim s2 As Double
  Dim s3 As Double
  Dim a1 As Double
  Dim a2 As Double
  Dim a3 As Double
  s3 = aa(4)
  a1 = aa(1)
  a2 = aa(3)
  a3 = 180 - a1 - a2
  a2 = (a2/360)*2*cPI
  a1 = (a1/360)*2*cPI
  a3 = (a3/360)*2*cPI

  s2 = s3 * Sin(a3) / Sin(a1)
  s1 = s3 * Sin(a2) / Sin(a1)

  a1 = 360*a1/(2*cPI)
  a2 = 360*a2/(2*cPI)
  a3 = 360*a3/(2*cPI)

  a1 = NumberFormat2(a1,0,3,3,False)
  a2 = NumberFormat2(a2,0,3,3,False)
  a3 = NumberFormat2(a3,0,3,3,False)
  s1 = NumberFormat2(s1,0,3,3,False)
  s2 = NumberFormat2(s2,0,3,3,False)
  s3 = NumberFormat2(s3,0,3,3,False)

  aa(0) = s1
  aa(1) = a1
  aa(2) = s2
  aa(3) = a2
  aa(4) = s3
  aa(5) = a3

  Return aa


End Sub

Figured it out - made corrections to the above and it now works
 
Last edited:
Upvote 0
What is the exact error message ?
In this code you already have an error:
Code: Copy
Sub Button3_Click
  Dim zz(5) As Double
  zz(0) = 0
  zz(1) = 90
  zz(2) = 0
  zz(3) = 30
  zz(4) = 2
  zz(5) = 0
  zz = JHSMATH.Triangle_AAS(zz)
End Sub
You dim zz(5) which means zz(0) to zz(4) five items.
zz(5) is one element too much !
 
Upvote 0
Back
Top