B4J Question Connection between subs

Arturs

Member
Licensed User
Longtime User
Hi

Is it possbile to know from which subs another sub was called ?

For example

B4X:
Sub Test

If the sub was called from Sub1 then

do it the code

else

do it the code

endif

End Sub

Regards
Artur
 

Cableguy

Expert
Licensed User
Longtime User
not directly, but you can set a flag ( variable ) and set at each sub call (if it a control triggered on you can use sender, or if it is a custom sub, you can pass the calling sub as a string parameter)

examples:
B4X:
sub globals
....
dim CallingSub as string
....
end sub

sub test
CallingSub = "test"
callsub("test2")
end sub

sub test2
log(CallingSub)
endsub

B4X:
sub Button_Clicked
CallingSub=sender
log(CallingSub)
end sub
B4X:
sub test
test2("test") ' this will call test2 sub passing "test" as parameter
end sub

sub test2(CallerSub)
log(CallerSub)
end sub
 
Upvote 0

Arturs

Member
Licensed User
Longtime User
B4X:
Sub Button1_MouseClicked (EventData As MouseEvent)   
    btn = Sender
   Log(btn)
     
End Sub

and another Event I want to check value of sender

B4X:
If btn = here should be name of button to check which buttons were used ?
 
Upvote 0

Arturs

Member
Licensed User
Longtime User
Actually If I press Button1 I would like to forward all traffic from Bluetooth to Edit1
If I press Button2 I would like to forward all traffic from BT to Edit2
 
Upvote 0

KMatle

Expert
Licensed User
Longtime User
I assume you're a beginner.

If you have 2 different Buttons (B1, B2) then you have 2 corresponding click-event-subs. So it's easy to know which one it was.

The "sender-thing" is not needed for 2 just buttons (I assume you create them in the designer?)

Hint: Take a look to the beginners guide. Of course we will guide you...
 
Upvote 0

Arturs

Member
Licensed User
Longtime User
Yes. You are right but I did not mean it.
I will try to explain it more precisely.

I have event which is called automaticaly when new information were received from another device.
The event must know which button was last pressed and forward the information to proper EditText.
In my case Button is something like flag.
I can set a flag in variables According to Cableguy advice and It works :)
but I think the solution with Sender is better.

B4X:
Sub Button1_MouseClicked (EventData As MouseEvent)  
    btn = Sender
   Log(btn)
    
End Sub

In above code I take Sender into btn variable.
I would like to check the variable in Bluetooth event but I do not know how to use btn in IF function.
Should I compare btn with name of Button ?

B4X:
If btn = here should be name of button to check which buttons were used ?

Regards
Artur
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
in the btn control, define the tag property with a meaningful string, like "buton1" or "stream1", then using the sender, like you did, just check the tag, like:
btn=sender
if btn.tag = "stream1" then....

or use a select case, like

select btn.tag

case "stream1"
your code goes here
case "stream2"
your code goes here
end select
 
Last edited:
Upvote 0

Arturs

Member
Licensed User
Longtime User
Is it possbile to wait in a sub as long as new information will be received from BT. I tried to use "Do While" but it does not work because event BT is called after the loop.

Summary:
I need to
- send first information
- wait for answer
- after receiving answer send second information
- wait for answer
....
 
Upvote 0

Arturs

Member
Licensed User
Longtime User
OK.

For example I want to send 5 commands to my device

send_command(1)
send_command(2)
send_command(3)
send_command(4)
send_command(5)


If I sent one by one it does not work because the command are sent faster then new_inforormation are received so my application skips some commands.

My solution is create a buffer with commands and then send one by one but In the way I need thread where the aplication will wait for answer and sends next command from buffer.

What do you think about my solution ? Is it better way to do it in B4J or B4A ?
 
Upvote 0

Arturs

Member
Licensed User
Longtime User
I use AsyncStreams for BT and udppacketarrived for WIFI. I am more intrested in how to do it with udppacketarrived events
 
Upvote 0
Top