Calling a Sub in Code module

rfresh

Well-Known Member
Licensed User
Longtime User
I have a Code module that I want to share a function with two Activities. I want to get the Activity.Width and Activity.Height values.

I have this in my Code module:

B4X:
Sub Load_Screen_Resolution(Activity)
   If Activity.Width > Activity.Height Then
           ' do something
        End If

and I am calling it from my Main module like this:

B4X:
Load_Screen_Resolution(Activity)

But the compiler doesn't like it. I thought I could call Load_Screen_Resolution() from Main and my Setup activity like so:

B4X:
'from Main Activity
Load_Screen_Resolution(Activity)
'from Setup Activity
Load_Screen_Resolution(Activity)

I guess I'm not doing this right.
 

rfresh

Well-Known Member
Licensed User
Longtime User
Ok I added that so the call is now:

B4X:
Code.Load_Screen_Resolution(Activity)

Now the compiler is complaining about the property .Width in the If statement:

B4X:
Sub Load_Screen_Resolution(Activity)
   If Activity.Width > Activity.Height Then

So apparently Activity isn't being 'seen' as coming from Main.
 
Upvote 0

JonPM

Well-Known Member
Licensed User
Longtime User
What if you removed Activity?
Sub Load_Screen_Resolution
....
End Sub

Code.Load_Screen_Resolution
 
Upvote 0

NJDude

Expert
Licensed User
Longtime User
You have to pass the values to the Sub, like this:
B4X:
Sub Load_Screen_Resolution(aWidth as int, aHeight as Int)
    If aWidth > aHeight Then ...

Calling the Sub:
B4X:
Code.Load_Screen_Resolution(Activity.Width, Activity.Height)
 
Upvote 0

rfresh

Well-Known Member
Licensed User
Longtime User
@NJDude

Ok that worked. My last hurdle to clear is calling a Sub in the Code module that is in Main. The same Sub will be in my Setup module so in Code the call will have to go to either Main or Setup modules:

B4X:
Sub Load_Screen_Resolution(aScreenWidth As Int, aScreenHeight As Int)
   If aScreenWidth = 960 AND aScreenHeight = 540 Then

   Load_Landscape_960x540x240_Screen_Resolution

        'the above line is red and is in the Main module. But Main. doesn't work,
        'the compiler says unknown member
   End If

I need to be able to call this sub above Load_landscape...() in either the Main or the Setup modules.
 
Upvote 0

rbsoft

Active Member
Licensed User
Longtime User
Check in the help for CallSub.

In this case you would use:
CallSub(Main, "Load_Landscape_960x540x240_Screen_Resolution")

Rolf
 
Upvote 0

rfresh

Well-Known Member
Licensed User
Longtime User
@rbsoft

How do I step into the CallSub(Main...) with the debugger?

I ran the debugger but it didn't step into the called sub routine CallSub(Main, "Load_Landscape_960x540x240_Screen_Resolution" )
 
Upvote 0

johnB

Active Member
Licensed User
Longtime User
Hi
I have the same problem as post 8 but the post didn't get an answer
I want to call a Sub in Main from a code module but doesn't get called when following it in the Debug(rapid).
This is an old post so maybe "rfresh" found the answer but didn't post it
Thanks
 
Upvote 0
Top