Optional Integer Argument

If a method takes and Optional Integer Argument i.e.:

Public MyMethod(Optional arg as integer)

If no value is passed into arg when calling the method, what value is arg in the method? Should it be Nil?

I have the above code and in the method I am testing If arg <> Nil then but getting type mismatch expected integer but got nil

[quote=150112:@Mike Charlesworth]If a method takes and Optional Integer Argument i.e.:

Public MyMethod(Optional arg as integer)

If no value is passed into arg when calling the method, what value is arg in the method? Should it be Nil?

I have the above code and in the method I am testing If arg <> Nil then but getting type mismatch expected integer but got nil[/quote]

Don’t test for nil. if no value is passed, arg is zero.

That said, I do not know how to test if the calling code passed a variable or not.

For giggles, see if it’s 0.
I always try to define my optional arguments so I know what it is in the event I option not to include the argument.

Public MyMethod(arg as Integer = 42)

This works just like an optional, but then I know it will be 42 if nothing is sent to it.

Ohp, looks like I was beaten to the punch.

I found a way : instead of integer, use a variant, and test with variant.IsNull.

I could set a default of -1 as I know the passed arg will not ever be -1 so as @Tim Parnell says this will tell me nothing has been passed.

I could but i’m not a fan of variants or rather I prefer to use them as little as possible. And with arguments you run the risk of passing a value which needs testing in the method then to ensure it is of the correct type.

Thanks folks, i think i will go for the -1 default.

Would the new AUTO type be useful here? Would it offer more type safety than variant? Just asking, haven’t yet used it.

Auto is safer since it does not allow implicit conversion. But the solution of the default value of -1 (or whichever other negative value such as -32,768) is probably the best one. Better use an integer to do the job of an integer :wink:

Thanks Michel. I tend to agree with using a hammer when you have a nail. An AUTO may satisfy the desire to check for a nil value though, which can’t be done on an integer.

I believe an Auto can’t be used for that.

Yes you are right it does work. I could still assign a duff value to it though and not throw an exception until runtime.

Seems time to download the new version to test things.

Why, it works fine in my testing

Downloaded and tested works fine

Because I thought wrongly too fast. I thought something like “Auto will assume a non integer and will crash the math” or something.

Not if you test the datatype at the beginning of the method.

If it is passed a value with no decimal, an auto will do an implicit assignment as integer, just like a constant would.

Why not have one method that accepts a value and one that does not?
Then do the work accordingly. Or have the no parameter method call that parametered method with a calculated value.