Android Tutorial Static Code Modules

Johan Schoeman

Expert
Licensed User
Longtime User
Hi Erel

Busy playing around with code modules. Why has ButtonsCreator been declared as type Button in your above example if ButtonsCreator is never used inside the sub as a variable (as is the case with a fuction in for eg VBA where the function name returns a value and therefore the type that the function has been declared as)?

Sub ButtonsCreator(Text AsString) As Button
 

Johan Schoeman

Expert
Licensed User
Longtime User
Hi Klaus

So if we do the following - for whatever stupid reason whatsoever!:

Sub ButtonsCreator(Text As String) As Button
Dim answer as Double
...code
...code
...code
Return answer

This will return a type Double and not a type Button. Is that correct? It means we can return any type depending on what type "answer" has been declared as?
 

klaus

Expert
Licensed User
Longtime User
Your example is wrong:
Sub ButtonsCreator(Text As String) As Button
Dim answer as Double
...code
...code
...code
Return answer

because the return object doesn't match the return object definition and you will get an error with it.

But this code will work correctly:
Sub ButtonsCreator(Text As String) As Double
Dim answer as Double
...code
...code
...code
Return answer

Yes you can return any object with a routine even arrays, but the return object must match the declaration.
One dimension array:
Sub MyRoutine(Text As String) As Double()
Dim answer(5) As Double
Two dimensions array:
Sub MyRoutine(Text As String) As Double(,)
Dim answer(5, 5) As Double
 

Johan Schoeman

Expert
Licensed User
Longtime User
The below works perfectly. GreatCircleDistance declared as type String but returning Distance declared as type Float? No error encountered....

B4X:
Public Sub GreatCircleDistance (lat_d1 As Float, lat_m1 As Float, lat_s1 As Float, lon_d1 As Float, lon_m1 As Float, lon_s1 As Float, _
                                lat_d2 As Float, lat_m2 As Float, lat_s2 As Float, lon_d2 As Float, lon_m2 As Float, lon_s2 As Float) As String
 
Dim Distance As Float
 
....code
....code
....code
Return Distance
   
End Sub
 

klaus

Expert
Licensed User
Longtime User
I think that the String object is a bit special in B4A, variables are probably tranformed internally to match the declaration.
I 'm almost sure that with a view it will not work.
You can also define the return type as Object.
For me, matching the object types is normal programming practice.
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…