Some clarification for callsub and log

jschuchert

Active Member
Licensed User
Longtime User
I have a few subroutines (some are quite long) that will be used in several activities. Currently, I am simply copying/pasting/editing them from the b4ppc version of my application into the needed activity. There has to be a better way.

I am confused by the documentation for "Callsub". I experimented with some simple subroutines that do nothing more than call for a msgbox but they don't work. For example I have an activity named "input" and another named "curves". I put a sub named 'test' in the 'input' activity like this:
sub test
msgbox("hello","message")
end sub

then in the curves 'btnOK_click' event I put:
sub btnOK_click
Callsub(input,"test")
end sub

Nothing happened or no error warning.

kickaha coded an example for me how to retrieve a sub routine from a Code module but I have not been able to duplicate it with any of my more complex routines. According to the documentation, you are not supposed to be able to use the code module like this but I was hopeful the callsub would work between activities.

I don't understand how log (x) works. Again I tried a simple experiment like shown in the docs:
For i = 1 to 10
log(i)
next
then looked in the 'logs' in the lower right of the ide but could not see the result.

In both cases perhaps the code skipped over it although it was placed where it should have been recognized. I am a 'sledgehammer' type of guy who will use whatever works so I can live with what I am doing but I would really like to use a carpenter's hammer once in a while. i appreciate all of you.

Jim Schuchert
 

agraham

Expert
Licensed User
Longtime User
From the Service modules tutorial - my boldening.

Using CallSub method you can also call a sub in a different module.
It is however limited to non-paused modules. This means that one activity can never access a sub of a different activity as there could only be one running activity.

For Log have you pressed Connect on the Logs tab first?
 
Upvote 0

jschuchert

Active Member
Licensed User
Longtime User
Thank you, Andrew. It was a 'duh' moment when I finally noted the connect button in 'logs'.

I had previously reviewed the service module docs but they went over my head. I will revisit them and try to understand better. I believe terminology is my biggest problem. Would you provide a "very simple" example like in the following scenario:
' a sub named test '
sub test
dim x,y,z
z=x*y 'x and y are called from text boxes
end sub

'another sub named test2
sub test2
dim t
t = z
end sub

I want to use the result from test in test2. Of course, in this case I could put them in the same activity module but play like test is much more complex and will be used in several activities. How would you handle this using a service module? My experiments did not work.

Thank you very much for your time and expertise.
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
I have a few subroutines (some are quite long) that will be used in several activities. Currently, I am simply copying/pasting/editing them from the b4ppc version of my application into the needed activity. There has to be a better way.
If this is really what you want then just put them in a Code module and call them from your activities.
 
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
Hi Jim,

What you need to do is create a Code module (Project>Add new module>code module), I will presume you called it General in the rest of this message.

Now you can put all the subs that you want to call from your activities in General.
B4X:
'Code module
'Subs in this code module will be accessible from all modules.
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.

End Sub
' a sub named test ' 
sub test
dim x,y,z
z=x*y 'x and y are called from text boxes
end sub

'another sub named test2
sub test2
dim t
t = z 
end sub
Now you can call test from an activity with General.test, and test2 by General.test2
calling them within the code module is just by their name (so to call test from test2, just use test:

B4X:
'Code module
'Subs in this code module will be accessible from all modules.
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.

End Sub

sub calcnumber (value As EditText)' you can pass objects like this so that you can manipulate the contents
v=value.Text
t=test2 (v)' call test2 to multiply by 4
value.Text=t
end sub

sub test2 (value As Int)' just does *4
result=value*4
return result
end sub
You would call this from an activity with
B4X:
General.calcnumber (someeditbox)
and the contents of someeditbox would get multiplied by 4 (assuming it was a valid number). I realise that the example is pointless, but it is just to illustrate how to use code modules, hope that helps a bit.

Just read your mail, you can send me the code with questions if you wish, but ultimately you get a better spread of answers (along with some REAL experts) by posting on here.
 
Upvote 0

jschuchert

Active Member
Licensed User
Longtime User
Thanks a lot, Andy, for your detailed explanation. That will help a bunch to clear it up. I was under the impression that code modules could not be called from activities but now I see much better how they can.

In B4ppc I used 'goto' to a label inserted just before 'end sub' to exit the sub after a calculation was completed and I did not want the code to continue. It did not necessarily fall within 'end if' or other conditional loops. What would be the best method to handle in in B4a?

I tried to use sqrt(variable^2 + anothervariable^2) but it choked, so I had to use sqrt(variable*variable + anothervariable*anothervariable). Maybe the ^ is not recognized.

Thanks, as always, for your excellent assistance.

BTW if you replied to my email, I didn't receive it.

Jim
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
In B4ppc I used 'goto' to a label inserted just before 'end sub' to exit the sub after a calculation was completed and I did not want the code to continue. It did not necessarily fall within 'end if' or other conditional loops. What would be the best method to handle in in B4a?
For this it would be easier if you post the code of the routine.

I tried to use sqrt(variable^2 + anothervariable^2) but it choked, so I had to use sqrt(variable*variable + anothervariable*anothervariable). Maybe the ^ is not recognized.
You are right ^ is not recognized by B4A.
sqrt(variable^2 + anothervariable^2) becomes
Sqrt(Power(variable,2)+Power(anothervariable,2)) in B4A.

Best regards.
 
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
Jim,
To exit a sub just use Return (along with a value if you are expecting one to be returned!).

I have not yet replied to your mail, I have been rather busy at home for a few days.
 
Upvote 0

jschuchert

Active Member
Licensed User
Longtime User
Klaus, good to hear from you again. Thanks for the clarification about raising numbers to a power. when I went back through much of my code I saw that the 'goto' was contained within an 'end if' and where I used 'exit' to handle it. If I can find where it is otherwise, I will post it.

Thanks again.

Jim
 
Upvote 0
Top