Parameter With A Default Value

Public Function TestMethod(Num As Integer = 0, Data As String) As String
  Return Data
End Function

Use:
Dim TestStr As String = "Test"

1) TestStr = TestMethod(0, TestStr)  //This works
2) TestStr = TestMethod(TestStr) //Error below

Using the demo method above gives the following error when compiling;

1 Like

Optional parameters can only be the final parameter(s) in a method. For example, you can have:

Function TestMethod(Data as String, Index as Integer = 0)
Function TestMethod(Data as String, Right as Integer = 0, Left as Integer = 0)

…both of which will let you do:

TestMethod("elephant")

Incorrect.
Using the word “Optional” with a parameter must be at the end.

Even though a parameter with a default value is considered optional you can do it this way. I use methods like this on all project types. Only an issue in Android

According to the documentation, all optional parameters, whether they have a default value or not, are required to be at the end of the parameter list.

It’s not using the “Optional” key word

I’m astonished. The documentation definitely says they can only be present at the end of the parameter list… but sure enough, it looks like you can put optional parameters in the middle of the list. I’m using a pretty old version of Xojo (2020 v1.2) and your example actually work on my system.

Based on the error message in your screenshot, this is probably an issue with the Android compiler; you should file a bug report and make it clear what platform you are targeting. You should also file a bug report against the documentation, since the stated limitations on optional parameters appears to be incorrect.

Yeah, it’s only on Android. It works on any other project type and version.
I’d say it’s an issue with method signatures.

I thought it was obvious what platform I was targeting as it’s only in the Android channel.
I don’t think the documentation is wrong as it’s talking about the Optional keyword.

I don’t think the documentation is wrong as it’s talking about the Optional keyword.

My version of the documentation is reasonably clear, although I can see how it might be easily misinterpreted because the word “optional” is also the keyword Optional.

I thought it was obvious what platform I was targeting as it’s only in the Android channel.

It is possible to browse the forum without focussing on a specific channel, so I’m seeing all posts. :slight_smile:

1 Like