Optional parameter

How to check if an optional parameter exists?

DoThisThing(Optional aThing as Integer) If aThing = -1 then //we do nothing at all else //let's do something with aThing end if

This doesn’t work? Is the value of aThing NIL if it doesn’t exist?

DoThisThing(Optional aThing as Integer = -1)
If aThing = -1 then
//we do nothing at all
else
//let’s do something with aThing
end if

Joachim, FYI, the way you wrote the code, the “Optional” keyword is not needed, but your suggestion is the way to do it now without taking extra steps.

In an upcoming version of Xojo, there will an Integer class (along with ones for all the types) that you can check for nil. You can simulate that right now by creating a class that acts like an Integer but can be set to nil.

If you want to forgo type-checking, you can also use a Variant (not recommended):

Function DoThisThing ( aThing As Variant = nil)
  if aThing is nil then ...

It seems to me, If the basic datatypes (particularly integers) becomes classes, that will likely introduce a lot of overhead and cause a big performance hit in certain situations.

“In addition to”, not “in replacement of”, and designed to address the issue mentioned by the OP.

Alexander, you have one other option that’s really only practical if there is just one parameter: Overload your method, one with the parameter and one without. In the latter case, you will know that the param wasn’t supplied and can act accordingly.

OK Thanks… I’ll take the Optional aThing as Integer = -1 route.