Android Question Solved: use 1 argument in 2 Subs

anOparator

Active Member
Licensed User
Longtime User
Am trying to use an argument from a Sub that has a CustomView,
B4X:
 Sub TabStrip1_PageSelected (Position As  Int)
Log($"Current page: ${Position}"$)
End Sub
Can the "Position" argument" be used in a different Sub?
B4X:
Sub Scoreboard
Label1.Text = $"${Position}"$
End Sub

And, if it is possible, what is this feature called.
thanks
 

Cableguy

Expert
Licensed User
Longtime User
Am trying to use an argument from a Sub that has a CustomView,
B4X:
 Sub TabStrip1_PageSelected (Position As  Int)
Log($"Current page: ${Position}"$)
End Sub
Can the "Position" argument" be used in a different Sub?
B4X:
Sub Scoreboard
Label1.Text = $"${Position}"$
End Sub

And, if it is possible, what is this feature called.
thanks

Create a global variable with a meaninfull name. (ie dim mPosition as int)
then...

B4X:
Sub TabStrip1_PageSelected (Position As  Int)
Log($"Current page: ${Position}"$)
mPosition = Position
End Sub

Sub Scoreboard
Label1.Text = $"${mPosition}"$End Sub
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
yes, but using a global variable enables to call the scoreboard at any time, and not in sequence with the sub that originated the value
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
yes, but if I understand it correctly, the scoreboard sub in this example is a dummy sub, and the real one may be used in more situations than just in sequence with the tabstrip...
If this example sub is a real one, then it would make more sense to just include the label.text change in the tabstrip event
 
Upvote 0

Ed Brown

Active Member
Licensed User
Longtime User
I prefer to pass the variable as an argument. This ensures that the value being passed has not changed to something else in the meantime or part way through the processing. ie. pass the value to guarantee that is does not change to something else that both the originator and receiver subs are not expecting. If it does change to a different value then it can have unwanted and unexpected effects/results.

This is more relevant to apps that use multi-threading but I find it good practice in general to pass the values instead of relying on global variables.
 
Upvote 0

anOparator

Active Member
Licensed User
Longtime User
Create a global variable with a meaninfull name. (ie dim mPosition as int)
then...

B4X:
Sub TabStrip1_PageSelected (Position As  Int)
Log($"Current page: ${Position}"$)
mPosition = Position
End Sub

Sub Scoreboard
Label1.Text = $"${mPosition}"$End Sub
thanks for the Global approach, I now see that I may be able to use it with "lbl" and "edt" instead of the arguments.
just doing my deja vu.
 
Upvote 0

anOparator

Active Member
Licensed User
Longtime User
I learned a lot from the feedback, thanks to all. Now I feel closer to resolving how to Update/Save additions to Labels.

Once I get to that point I hope I won't need keyvaluestore, but if needed I can handle kvs.
 

Attachments

  • tmp_2arguments-611319027.zip
    108.2 KB · Views: 139
Upvote 0
Top