I'm confused by the use of the Optional keyword in Sub definitions. I have tried to look through documentation (but you all know that story. ) and may have found something out of date or correct for a different version. In any case, my understanding of syntax that should be correct is roughly:
`Optional` varName `As Type = ` value
so for example:
`Optional msg As String = "Hello world"` *
When I try this in a LO macro, the compiler says: "BASIC syntax error. Expected )." and highlights the equals sign.
Undaunted, I remove the offending bit (' = "Hello world"'), leaving the 'Optional' keyword and trying to incorporate the check into the sub
`Optional msg As String`
...
if (msg = "") then
msg = "Hello world"
end if` **
The compiler then tells me: "BASIC runtime error. Argument is not optional." and selects the line with the if statement.
Can someone point me to what I'm doing wrong (or where the correct syntax is )?
Full method code:
* Ex 1:
Code: Select all
Sub TestOptional(Optional msg As String = "Hello world")
msgbox msg
End Sub
Code: Select all
Sub TestOptional(Optional msg As String)
if (msg ="") then
msg = "Hello World"
end if
msgbox msg
End Sub
Edit: Changed subject, was [Solved] Optional keyword Make your post understandable by others -- MrProgrammer, forum moderator |