Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 83 additions & 2 deletions docs/visual-basic/misc/bc30059.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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.
Copy link
Contributor

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.


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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.
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.


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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
### Alternative solutions
#### Option 1: Use ParamArray instead of Optional
### Solution 1: Use ParamArray instead of Optional

Copy link
Contributor

Choose a reason for hiding this comment

The 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#### Option 2: Use Nothing as default and initialize in the method body
### 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:

```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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#### Option 3: Provide an overload without the parameter
### Solution 3: Provide an overload without the parameter


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)
Loading