Skip to content

Commit c484d88

Browse files
CopilotBillWagnergewarren
authored
Enhance BC30059 documentation with array initialization guidance for optional parameters (#48258)
* Initial plan * Enhance BC30059 documentation with array initialization guidance Co-authored-by: BillWagner <[email protected]> * Apply suggestions from code review Co-authored-by: Genevieve Warren <[email protected]> * Change numbered list to bullets in BC30059 error correction steps Co-authored-by: BillWagner <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: BillWagner <[email protected]> Co-authored-by: Bill Wagner <[email protected]> Co-authored-by: Genevieve Warren <[email protected]>
1 parent baa8b69 commit c484d88

File tree

1 file changed

+83
-4
lines changed

1 file changed

+83
-4
lines changed

docs/visual-basic/misc/bc30059.md

Lines changed: 83 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,97 @@ ms.assetid: fdd5e7bb-6370-4a63-bbb6-23b15badb4c8
1111
---
1212
# Constant expression is required
1313

14-
A `Const` statement doesn't properly initialize a constant, or an array declaration uses a variable to specify the number of elements.
14+
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.
1515

1616
**Error ID:** BC30059
1717

1818
## To correct this error
1919

20-
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.
20+
- 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.
2121

22-
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.
22+
- 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.
23+
24+
- 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.
2325

24-
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.
26+
- 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.
27+
28+
## Array initialization in optional parameters
29+
30+
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:
31+
32+
```vb
33+
' This causes BC30059
34+
Public Function MyFun(Optional filters() As (String, String) = New (String, String)() {}) As Boolean
35+
' Function body
36+
End Function
37+
```
38+
39+
### Solution 1: Use ParamArray instead of Optional
40+
41+
If you need to accept a variable number of arguments, consider using `ParamArray` instead of an optional parameter:
42+
43+
```vb
44+
Public Function MyFun(ParamArray filters() As (String, String)) As Boolean
45+
' The ParamArray automatically provides an empty array if no arguments are passed
46+
For Each filter In filters
47+
' Process each filter
48+
Next
49+
Return True
50+
End Function
51+
52+
' Can be called with any number of arguments:
53+
MyFun() ' Empty array
54+
MyFun(("name", "value"))
55+
MyFun(("name1", "value1"), ("name2", "value2"))
56+
```
57+
58+
### Solution 2: Use Nothing as default and initialize in the method body
59+
60+
Set the default value to `Nothing` and check for it in your method:
61+
62+
```vb
63+
Public Function MyFun(Optional filters() As (String, String) = Nothing) As Boolean
64+
If filters Is Nothing Then
65+
filters = New (String, String)() {}
66+
End If
67+
68+
' Process the filters array
69+
For Each filter In filters
70+
' Process each filter
71+
Next
72+
Return True
73+
End Function
74+
75+
' Can be called without arguments:
76+
MyFun() ' Uses empty array
77+
' Or with an array:
78+
MyFun({("name", "value")})
79+
```
80+
81+
### Solution 3: Provide an overload without the parameter
82+
83+
Create an overloaded version of the method that doesn't require the parameter:
84+
85+
```vb
86+
' Overload without the parameter
87+
Public Function MyFun() As Boolean
88+
Return MyFun(New (String, String)() {})
89+
End Function
90+
91+
' Main method with required parameter
92+
Public Function MyFun(filters() As (String, String)) As Boolean
93+
' Process the filters array
94+
For Each filter In filters
95+
' Process each filter
96+
Next
97+
Return True
98+
End Function
99+
```
25100

26101
## See also
27102

28103
- [Const Statement](../language-reference/statements/const-statement.md)
104+
- [Optional Parameters](../programming-guide/language-features/procedures/optional-parameters.md)
105+
- [Parameter Arrays](../programming-guide/language-features/procedures/parameter-arrays.md)
106+
- [How to: Initialize an Array Variable in Visual Basic](../programming-guide/language-features/arrays/how-to-initialize-an-array-variable.md)
107+
- [Arrays in Visual Basic](../programming-guide/language-features/arrays/index.md)

0 commit comments

Comments
 (0)