vb.netfunctionparamarray

How to limit ParamArray Bounds In VB.NET?


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?


Solution

  • 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