-
Notifications
You must be signed in to change notification settings - Fork 6k
Enhance BC30059 documentation with array initialization guidance for optional parameters #48258
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -11,7 +11,7 @@ ms.assetid: fdd5e7bb-6370-4a63-bbb6-23b15badb4c8 | |||||||||
--- | ||||||||||
# Constant expression is required | ||||||||||
|
||||||||||
A `Const` statement doesn't properly initialize a constant, or an array declaration uses a variable to specify the number of elements. | ||||||||||
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 | ||||||||||
|
||||||||||
|
@@ -20,9 +20,90 @@ A `Const` statement doesn't properly initialize a constant, or an array declarat | |||||||||
1. If the declaration is a `Const` statement, 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. | ||||||||||
|
||||||||||
2. 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. | ||||||||||
|
||||||||||
3. 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](#array-initialization-in-optional-parameters) section below. | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
|
||||||||||
3. If the preceding checks don't address the issue, try setting the `Const` to a different temporary value, running the program, and then resetting the `Const` to the desired value. | ||||||||||
4. If the preceding checks don't address the issue, try setting the `Const` to a different temporary value, running the program, and then resetting the `Const` to 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: | ||||||||||
|
||||||||||
```vb | ||||||||||
' This causes BC30059 | ||||||||||
Public Function MyFun(Optional filters() As (String, String) = New (String, String)() {}) As Boolean | ||||||||||
' Function body | ||||||||||
End Function | ||||||||||
``` | ||||||||||
|
||||||||||
### Alternative solutions | ||||||||||
|
||||||||||
#### Option 1: Use ParamArray instead of Optional | ||||||||||
Comment on lines
+39
to
+41
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since there's no text between the H3 and H4s, seems like it's better to skip the "Alternative solutions" heading altogether. |
||||||||||
|
||||||||||
If you need to accept a variable number of arguments, consider using `ParamArray` instead of an optional parameter: | ||||||||||
|
||||||||||
```vb | ||||||||||
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")) | ||||||||||
``` | ||||||||||
|
||||||||||
#### Option 2: Use Nothing as default and initialize in the method body | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
|
||||||||||
Set the default value to `Nothing` and check for it in your method: | ||||||||||
|
||||||||||
```vb | ||||||||||
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")}) | ||||||||||
``` | ||||||||||
|
||||||||||
#### Option 3: Provide an overload without the parameter | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
|
||||||||||
Create an overloaded version of the method that doesn't require the parameter: | ||||||||||
|
||||||||||
```vb | ||||||||||
' 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 | ||||||||||
``` | ||||||||||
|
||||||||||
## See also | ||||||||||
|
||||||||||
- [Const Statement](../language-reference/statements/const-statement.md) | ||||||||||
- [Optional Parameters](../programming-guide/language-features/procedures/optional-parameters.md) | ||||||||||
- [Parameter Arrays](../programming-guide/language-features/procedures/parameter-arrays.md) | ||||||||||
- [How to: Initialize an Array Variable in Visual Basic](../programming-guide/language-features/arrays/how-to-initialize-an-array-variable.md) | ||||||||||
- [Arrays in Visual Basic](../programming-guide/language-features/arrays/index.md) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These should be bullets instead of numbered list according to style rules.