Android Question pass array of ints

cadcamman

Member
Licensed User
Longtime User
Hi Guys

Can anyone help, I have an array of integers and I am trying to write a sub to pass them to labels. I have tried using Label2.Text = NumberFormat(Num(b),1,0). and a few other methods but it always receives zero

Any help is appreciated

cadcamman
 

cadcamman

Member
Licensed User
Longtime User
Sorry, additional post was an error.

B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim temp As Int

    Dim i As Int = 0
    Dim Num(6) As Int
    Dim Label1 As Label
    Dim Label2 As Label
    Dim Label3 As Label
    Dim Label4 As Label
    Dim Label5 As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("Layout1")
    
        
Dim Results(61) As Boolean

Results(0) = True 'prevent it picking zero
Do While i < 6
   temp=Rnd(1,60)
   If Results(temp) = True Then Continue  ' send it back to pick that one again
   Results(temp)=True
   Num(i)=temp
   i=i+1
    pressButton1(i)
    
Loop




End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub




Sub pressButton1(b As Double)
    'Show the results
        

    Select b     
        Case 1
            Label1.Text =  NumberFormat(Num(b),1,0)
        Case 2
            Label2.Text = NumberFormat(Num(b),1,0)
    
        Case 3
            Label3.Text = NumberFormat(Num(b),1,0)
            
        Case 4
            Label4.Text = NumberFormat(Num(b),1,0)
    
        Case 5
            Label5.Text = NumberFormat(Num(b),1,0)
    End Select
End Sub
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
You are incrementing your loop index (i) before calling the pressButton1 sub. Swap the last 2 lines in your Do While loop.

- Colin.
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
Fyi - arrays are zero based (ie: the range for Num(6) is actually 0-5). The way your code is written you will never get the value from Num(0). If you want that, you will have to add a Case 0 statement to your pressButton1 sub. If you only want array elements 1-5, initialize i at 1 or set it to i = 1 before you start your Do While loop (actually if you want a 5 element array you should declare Num(5) & then reference elements 0-5).

Also (sorry, being pedantic here) i & temp can be declared locally in Activity_Create because they are not referenced outside that sub.

Not trying to be critical - just offering some advice based on personal experience.

- Colin.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…