I had two really weird bugs today. It was with a Function I adjusted. This is the concept:
getSomething(param1 As Integer = -1, param2 As Integer = -1, param3 As Integer = -1, param4 As Integer = -1, param5 As Variant) As String
Yet I was able to compile OK and call the function using:
newString = getSomething(1, 2, 3)
I should’t have been allowed to call the Function without defining param5 since it has no default. This is bug #1.
When the Function does run, it sets param1 and param2 OK, but param3 = -1 and param5 = 3, whereas I would expect param3 = 3, param4 = -1 and param5 = nil. This is Bug #2.
Before I submit it into Feedback, am I not understanding something here?
If you want the method to behave like expected, you have to make sure the parameter requirements are fulfilled.
param5 is not optional, so the compiler knows the last of the specified method properties must be the Variant param5. As the rest is optional and you pass number values, the compiler assigns them as Integers to param1 and param2.
If you want to avoid this apparent parameter skipping, either make param5 optional too like Bogdan said or call the method with
Because your input fulfills the requirements of your method parameters distinctly:
param1 param4 have to be Integers, but they are optional. param5 must be present, but it is a variant which can also be an Integer of course. Because it must be present and 3 is a variant too, the compiler assigns 3 to param5, 1 to param1, 2 to param2 and fills the rest with -1s.
It would be different if param5 were an object, a control or anything except a number. Or if it would be optional too. In these cases, the compiler would assign the variables as you expected.
In any case, there is no need for a bug report. Like Christian said, your expectations did not match the way the compiler works, but there is nothing wrong with it.*
one more thing but don’t know if it’s possible with xojo to do is to call by name input variable and after then to set value of that.
for this you should see tech. doc. or to contact some of xojo people.
in your case
getSomething(param1 As Integer = -1, param2 As Integer = -1, param3 As Integer = -1, param4 As Integer = -1, param5 As Variant) As String
[quote=399218:@Bogdan Pavlovic]one more thing but don’t know if it’s possible with xojo to do is to call by name input variable and after then to set value of that.
for this you should see tech. doc. or to contact some of xojo people.
in your case
getSomething(param1 As Integer = -1, param2 As Integer = -1, param3 As Integer = -1, param4 As Integer = -1, param5 As Variant) As String
As another example a method from a declare library I published recently:
Protected Function PowerData(node as int32 = 0, msr as int32) As double()
// code stripped
End Function
I usually specify only the msr parameter, because if I dont pass two Ints, node will automatically be 0. This is perfectly reliable, although it might be confusing for the user of the library.
EDIT:
This is equivalent to a method override:
Protected Function PowerData(node as int32, msr as int32) As double()
// code stripped
End Function
Protected Function PowerData(msr as int32) As double()
Return PowerData(0, msr)
End Function