Gianni Sassanelli Active Member Licensed User Longtime User Aug 18, 2020 #1 Hi I use most of time, string parameter in my sub For example B4X: Copy 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
Hi I use most of time, string parameter in my sub For example B4X: Copy 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
angel_ Well-Known Member Licensed User Longtime User Aug 18, 2020 #2 Try this B4X: Copy CallSubDelayed2(Me,"mySub", (64+5) & "") Upvote 0
Star-Dust Expert Licensed User Longtime User Aug 18, 2020 #3 try this B4X: Copy CallSubDelayed2(Me,"mySub", $"$1.0{(64+5)}"$) Or this, but I consider it less clean B4X: Copy CallSubDelayed2(Me,"mySub","" & (64+5)) Upvote 0
try this B4X: Copy CallSubDelayed2(Me,"mySub", $"$1.0{(64+5)}"$) Or this, but I consider it less clean B4X: Copy CallSubDelayed2(Me,"mySub","" & (64+5))
Gianni Sassanelli Active Member Licensed User Longtime User Aug 18, 2020 #4 Star-Dust said: try this B4X: Copy CallSubDelayed2(Me,"mySub", $"$1.0{(64+5)}"$) Or this, but I consider it less clean B4X: Copy CallSubDelayed2(Me,"mySub","" & (64+5)) Click to expand... 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 said: try this B4X: Copy CallSubDelayed2(Me,"mySub", $"$1.0{(64+5)}"$) Or this, but I consider it less clean B4X: Copy CallSubDelayed2(Me,"mySub","" & (64+5)) Click to expand... 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?
Star-Dust Expert Licensed User Longtime User Aug 18, 2020 #5 Gianni Sassanelli said: 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? Click to expand... 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: Copy Dim MyString as String = MyGenercObject CallSub (Name, MyString) B4X: Copy Dim MyInt as Int= MyGenercObject CallSub (Name, MyInt) B4X: Copy Dim MyDouble as Double= MyGenercObject CallSub (Name, MyDouble) Upvote 0
Gianni Sassanelli said: 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? Click to expand... 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: Copy Dim MyString as String = MyGenercObject CallSub (Name, MyString) B4X: Copy Dim MyInt as Int= MyGenercObject CallSub (Name, MyInt) B4X: Copy Dim MyDouble as Double= MyGenercObject CallSub (Name, MyDouble)
LucaMs Expert Licensed User Longtime User Aug 18, 2020 #6 Star-Dust said: 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 Click to expand... Star-Dust said: Unfortunately in B4A this feature does not exist Click to expand... This: Star-Dust said: MySub (String Variable) Click to expand... is exactly the same of his b4a routine: Gianni Sassanelli said: Sub mySub(sPar as String) Click to expand... 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: Aug 18, 2020 Upvote 0
Star-Dust said: 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 Click to expand... Star-Dust said: Unfortunately in B4A this feature does not exist Click to expand... This: Star-Dust said: MySub (String Variable) Click to expand... is exactly the same of his b4a routine: Gianni Sassanelli said: Sub mySub(sPar as String) Click to expand... 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.
M Mahares Expert Licensed User Longtime User Aug 18, 2020 #7 Here is my take using Resumable Sub, a different way but not any better than what was given above: Code: Copy 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
Here is my take using Resumable Sub, a different way but not any better than what was given above: Code: Copy 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
Star-Dust Expert Licensed User Longtime User Aug 18, 2020 #8 LucaMs said: This: is exactly the same of his b4a routine: Sub mySub(sPar as String) Click to expand... I was not referring to the declaration of a method but to the invocation of a method Eg: B4X: Copy Dim C as int = 10 MySub(String C) Sub MySub(Var As String) End SUB Upvote 0
LucaMs said: This: is exactly the same of his b4a routine: Sub mySub(sPar as String) Click to expand... I was not referring to the declaration of a method but to the invocation of a method Eg: B4X: Copy Dim C as int = 10 MySub(String C) Sub MySub(Var As String) End SUB
toby Well-Known Member Licensed User Longtime User Aug 18, 2020 #9 Another way: B4X: Copy Dim s as string=64+5 CallSubDelayed2(Me,"mySub", s) Upvote 0
Erel B4X founder Staff member Licensed User Longtime User Aug 19, 2020 #10 Why are you using CallSubDelayed??? Code: Copy Sleep(0) MySub(s) Upvote 0