I was working on a function that accepts ParamArrays. But it should have at least one element and the maximum of 5 elements. I tried defining array bounds but got an error Array bounds cannot appear in type specifiers
.
So how can I do this?
I was working on a function that accepts ParamArrays. But it should have at least one element and the maximum of 5 elements. I tried defining array bounds but got an error Array bounds cannot appear in type specifiers
.
So how can I do this?
So how can I do this?
You can’t. Not statically at least. The only thing you can do is check inside the function and throw an exception (e.g. an ArgumentException
) when the wrong number of arguments is encountered.
In terms of API design this strikes me as weird, however. I don’t think a ParamArray
is the best solution in your case, precisely because you seem to have restrictions which are not well reflected by a ParamArray
.
Not knowing the context of your question, I would suggest doing what you can to make the function signature honor the contract you are requiring. For instance:
Public Sub Grover (cheese1 as Cheese, Optional cheese2 as Cheese = Nothing, Optional cheese3 as Cheese = Nothing, Optional cheese4 as Cheese = Nothing, Optional cheese5 as Cheese = Nothing)
If cheese1 Is Nothing Then
'throw
End If
For Each cheese in {cheese1, cheese2, cheese3, cheese4, cheese5}
If cheese IsNot Nothing Then
cheese.Snozzle()
End If
'or, in VB14 (as of Visual Studio 2015)
cheese?.Snozzle()
Next
End Sub