Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
A Const statement doesn't properly initialize a constant, an array declaration uses a variable to specify the number of elements, or you're trying to initialize an array as the default value for an optional parameter.
Error ID: BC30059
To correct this error
If the declaration is a
Conststatement, check to make sure the constant is initialized with a literal, a previously declared constant, an enumeration member, or a combination of literals, constants, and enumeration members combined with operators.If the declaration specifies an array, check to see if a variable is being used to specify the number of elements. If so, replace the variable with a constant expression.
If you're trying to initialize an array as the default value for an optional parameter, use one of the alternative approaches described in the Array initialization in optional parameters section.
If the preceding checks don't address the issue, try setting the
Constto a different temporary value, running the program, and then resetting theConstto the desired value.
Array initialization in optional parameters
You cannot initialize an array as the default value for an optional parameter because array initialization is not a constant expression. The following code generates BC30059:
' This causes BC30059
Public Function MyFun(Optional filters() As (String, String) = New (String, String)() {}) As Boolean
' Function body
End Function
Solution 1: Use ParamArray instead of Optional
If you need to accept a variable number of arguments, consider using ParamArray instead of an optional parameter:
Public Function MyFun(ParamArray filters() As (String, String)) As Boolean
' The ParamArray automatically provides an empty array if no arguments are passed
For Each filter In filters
' Process each filter
Next
Return True
End Function
' Can be called with any number of arguments:
MyFun() ' Empty array
MyFun(("name", "value"))
MyFun(("name1", "value1"), ("name2", "value2"))
Solution 2: Use Nothing as default and initialize in the method body
Set the default value to Nothing and check for it in your method:
Public Function MyFun(Optional filters() As (String, String) = Nothing) As Boolean
If filters Is Nothing Then
filters = New (String, String)() {}
End If
' Process the filters array
For Each filter In filters
' Process each filter
Next
Return True
End Function
' Can be called without arguments:
MyFun() ' Uses empty array
' Or with an array:
MyFun({("name", "value")})
Solution 3: Provide an overload without the parameter
Create an overloaded version of the method that doesn't require the parameter:
' Overload without the parameter
Public Function MyFun() As Boolean
Return MyFun(New (String, String)() {})
End Function
' Main method with required parameter
Public Function MyFun(filters() As (String, String)) As Boolean
' Process the filters array
For Each filter In filters
' Process each filter
Next
Return True
End Function