Passing through an optional parameter?

How would you deal with an optional parameter?

For example the string method Middle is defined as

result = stringVariable.Middle(start [,length])

so for my API1 based wrapper I turn it into

Public Function Middle(extends s as String, start as integer, Optional Length as Integer) as String

But how do I pass that through my own Middle method that has the same method signature but uses Mid:

  return s.mid( start, ??? )

Or in other words - how can I test that the optional parameter is there - or not?

I guess by using two method signatures …

I use the other way of declaring optional parameters, by setting them a default value:

Public Function Middle(extends s as String, start as integer, Length as Integer = -1) as String
If Length = -1 then Length = s.len

return s.mid( start, Length)
1 Like

Thanks, but that wasn’t the problem. The problem was that I don’t know if the Middle method is called with ONE or TWO parameters, so in the wrapper method you can’t call mid using Optional or a value that might not be there - you can only do that in the method signature.

the two signatures

Public Function Middle(extends s as String, start as integer, Optional Length as Integer)
Public Function Middle(extends s as String, start as integer, Length as Integer = -1)

are nearly identical, and both set Length to be optional,
The only difference is the first will have a length value = 0
The second will have it set to -1
In neither can you really tell if the user called the method with the optional value = the default in the signature
ie/ you cannot in either case tell if they wrote

dim s as string
dim m as string = s.Middle(0,0) // or dim m as string = s.Middle(0)
OR
dim m as string = s.Middle(0,-1) // or dim m as string = s.Middle(0)

It WILL exist since its not an reference type that can be nil