-
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?
Conversation
Co-authored-by: BillWagner <[email protected]>
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.
This is ready for final review.
@@ -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. |
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.
@@ -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 comment
The reason will be displayed to describe this comment to others. Learn more.
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. |
### Alternative solutions | ||
|
||
#### Option 1: Use ParamArray instead of Optional |
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.
### Alternative solutions | |
#### Option 1: Use ParamArray instead of Optional | |
### Solution 1: Use ParamArray instead of Optional |
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.
Since there's no text between the H3 and H4s, seems like it's better to skip the "Alternative solutions" heading altogether.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
#### Option 2: Use Nothing as default and initialize in the method body | |
### Solution 2: Use Nothing as default and initialize in the method body |
MyFun({("name", "value")}) | ||
``` | ||
|
||
#### Option 3: Provide an overload without the parameter |
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.
#### Option 3: Provide an overload without the parameter | |
### Solution 3: Provide an overload without the parameter |
The BC30059 error documentation was missing guidance for developers encountering this error when trying to initialize arrays as default values for optional parameters. This is a common scenario that wasn't adequately covered in the existing documentation.
Changes Made
Enhanced
docs/visual-basic/misc/bc30059.md
with:ParamArray
instead of optional parametersNothing
and initializing in the method bodyProblem Addressed
The original issue showed code like this failing with BC30059:
The documentation now clearly explains why this fails (array initialization is not a constant expression) and provides multiple working alternatives that achieve the same goal.
Validation
Fixes #20882.
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.
Internal previews