Skip to content

Commit a5ac7d0

Browse files
CopilotBillWagner
andcommitted
Enhance BC30059 documentation with array initialization guidance
Co-authored-by: BillWagner <[email protected]>
1 parent 007a752 commit a5ac7d0

File tree

1 file changed

+83
-2
lines changed

1 file changed

+83
-2
lines changed

docs/visual-basic/misc/bc30059.md

Lines changed: 83 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ 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

@@ -20,9 +20,90 @@ A `Const` statement doesn't properly initialize a constant, or an array declarat
2020
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.
2121

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

26103
## See also
27104

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

0 commit comments

Comments
 (0)