Android Question Set numerous variables to a specific number or text, Must be simple but I'm brain dead

Colin Evans

Active Member
Licensed User
Longtime User
Hi, I'm sure it can be done but I can't seem to find an example or the way to do it. I have a number of variables, PS1, PS2, PS3, PS4, PS5 etc and I want to set them all to '0' at present I do it line by line eg.
PS1 = 0
PS2 = 0
PS3 = 0
PS4 = 0
PS5 = 0

I'm sure there must be a way to set them all to 0 on one line, something like

(PS1, PS2, PS3, PS4, PS5) = 0

Just another stumbling block I'm frustrated with as I know I've seen it done
 

DonManfred

Expert
Licensed User
Longtime User
You just can do it with new Objectdeklarations.
B4X:
dim ps1, ps2 , ps3, ps4, ps5 as Int = 0
If you want to update global variables you need to use a sub to set them all to a new value.
B4X:
private Sub resetvalues
PS1 = 0
PS2 = 0
PS3 = 0
PS4 = 0
PS5 = 0
end sub

probably better to use a Map to hold the variables/values.
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
just declare your variables as int. they are automatically initialized to 0 in b4x.
 
Upvote 0

Colin Evans

Active Member
Licensed User
Longtime User
Cheers for the replies, they are global variables and need to be reset during the game, it would be nice if you could set variable names like you could in good old VB6 as an array.

May be this is something that could be developed in the designer, i.e. when you create a variable in VB6 say (PS1)in the designer and then copy and paste it asks if it's an array, if you said yes the array would be created and the original variable would be PS1(0) and the copied one PS1(1) subsequent copies and pastes would continue the array PS1(2), PS1(3) etc

Therefore in B4A etc to change would simply be a for next loop

for i = 0 to 3
PS1(i) =0
next
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
or with javaobject
B4X:
    Dim Arrays As JavaObject
    Arrays.InitializeStatic("java.util.Arrays").RunMethod("fill",Array(PS1,0))
then you dont need to know the size for the loop or even have a loop
 
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
A small sub could set the array to any constant.

B4X:
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    
    Dim p(6) As Int        'all are zero
    setAllTo(p, 9)
    Log(p(5))        '9
End Sub

Private Sub setAllTo(ar() As Int, cnst As Int)
    For i = 0 To ar.Length -1
        ar(i) = cnst
    Next
End Sub
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
probably better to use a Map to hold the variables/values.
I agree. Maps are almost always the most useful structures (the only exception, in my opinion, are Lists of Custom Types, which have the advantage of sorting).


[This is almost certainly not the case, but a "fascinating" thing, now archaic because we can afford to throw away tons of memory, are operations on bits].
 
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
And @aeric answer works with redimensioning too.
B4X:
    Dim PS1 = 9, PS2 = 9, PS3 = 9, PS4 = 9, PS5 = 9 As Int
    PS3 = 17
    Log(PS3)    '17
    Dim PS1 = 9, PS2 = 9, PS3 = 9, PS4 = 9, PS5 = 9 As Int
    Log(PS3)    '9
 
Upvote 0

Colin Evans

Active Member
Licensed User
Longtime User
could you explain how I'd use this method please
 
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
"fill" is a Java method on Arrays. See example below.
(This is two lines. The call to "setAllTo" in post #9 is only one line, and is B4X syntax, instead of Java.)

B4X:
    Dim p(6) As Int        'all are zero
    Dim Arrays As JavaObject
    Arrays.InitializeStatic("java.util.Arrays").RunMethod("fill",Array(p,999))
    Log(p(3))        '999
 
Last edited:
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
If you bend the rules a bit it can be one line
B4X:
Null.As(JavaObject).InitializeStatic("java.util.Arrays").RunMethod("fill",Array(ps,9))

its just the last two parameters that are important ps and 9
ps is the array name you want to set to known values
9 is the value you want each ps(0) ... ps(n) to hold.
 
Last edited:
Upvote 0

MrKim

Well-Known Member
Licensed User
Longtime User
If you really want to keep the variables then store them in a map or a list and then later you can loop through that to set the variables.
B4X:
    Dim A, B, C As Int
    Dim M As Map
    M.Put(A, A)
    M.Put(B, B)
    M.Put(C, C)
    For Each Z As Int In M.Values
        Z = 0
    Next
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…