Android Question Implicit CAST Integer to String not work some time

Gianni Sassanelli

Active Member
Licensed User
Longtime User
Hi
I use most of time, string parameter in my sub
For example
B4X:
Sub mySub(sPar as String)
workMode = sPar                      'Dim WorkMode As Int    is declared in Sub GLOBALS of activity
end Sub

' I call my sub with this code
CallSubDelayed2(Me,"mySub", 64+5)   ' this work fine in the most of time but some time i have an exception
java.lang.IllegalArgumentException: method it.techservice.tpick.mySub argument 1 has type java.lang.String, got java.lang.Integer

how can i avoid this behavior?
thanks
 

Star-Dust

Expert
Licensed User
Longtime User
try this
B4X:
CallSubDelayed2(Me,"mySub", $"$1.0{(64+5)}"$)


Or this, but I consider it less clean
B4X:
CallSubDelayed2(Me,"mySub","" & (64+5))
 
Upvote 0

Gianni Sassanelli

Active Member
Licensed User
Longtime User

Thank you Star-Dust
i have resolved with your second example, but dubt remain.
Why some time the cast work and in other time not?
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
Thank you Star-Dust
i have resolved with your second example, but dubt remain.
Why some time the cast work and in other time not?
B4A is based on java. In java sometimes variable casting needs to be clarified, because you can use a variable in different ways.
In this case it's an Object, so you don't identify the variable type. In java to clarify you would write: MySub (String Variable). By preceding the type to the variable name (or data) you clearly indicate how you want to understand the data.

Unfortunately in B4A this feature does not exist and therefore to specify that that data is a string (or an integer or float) you have to use different means.

A method for strings is what you have seen, but a general method is like this:

B4X:
Dim MyString as String = MyGenercObject
CallSub (Name, MyString)

B4X:
Dim MyInt as Int= MyGenercObject

CallSub (Name, MyInt)

B4X:
Dim MyDouble as Double= MyGenercObject

CallSub (Name, MyDouble)
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
so you don't identify the variable type. In java to clarify you would write: MySub (String Variable). By preceding the type to the variable name (or data) you clearly indicate how you want to understand the data
Unfortunately in B4A this feature does not exist
This:
MySub (String Variable)
is exactly the same of his b4a routine:
Sub mySub(sPar as String)

I don't know if-why-when the casting results incorrect; anyway, you should not use a String as parameter in your routine if you have the "need" to pass other types to it, declare sPar As Object.
 
Last edited:
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Here is my take using Resumable Sub, a different way but not any better than what was given above:
B4X:
wait for (mySub(64+5)) Complete (res As Int)
 Log(workMode)  'display 69

Sub mySub(sPar As Object) As ResumableSub
    workMode = sPar   
    Return workMode   'Dim WorkMode As Int    is declared in Sub GLOBALS of activity
End Sub
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
This:

is exactly the same of his b4a routine:
Sub mySub(sPar as String)
I was not referring to the declaration of a method but to the invocation of a method

Eg:
B4X:
Dim C as int = 10
MySub(String C)


Sub MySub(Var As String)

End SUB
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…