Const 문이 상수를 제대로 초기화하지 않거나, 배열 선언에서 변수를 사용하여 요소 수를 지정하거나, 선택적 매개 변수의 기본값으로 배열을 초기화하려고 합니다.
오류 ID: BC30059
이 오류를 해결하려면
선언이 문인
Const경우 상수가 리터럴, 이전에 선언된 상수, 열거형 멤버 또는 연산자와 결합된 리터럴, 상수 및 열거형 멤버의 조합으로 초기화되었는지 확인합니다.선언에서 배열을 지정하는 경우 변수가 요소 수를 지정하는 데 사용되는지 확인합니다. 그렇다면 변수를 상수 식으로 바꿉다.
선택적 매개 변수의 기본값으로 배열을 초기화하려는 경우 선택적 매개 변수 섹션의 배열 초기화에 설명된 대체 방법 중 하나를 사용합니다.
위의 검사에서 문제를 해결하지 못하는 경우 다른 임시 값으로 설정하고
Const프로그램을 실행한 다음 원하는 값으로 다시 설정Const해 보세요.
선택적 매개 변수의 배열 초기화
배열 초기화는 상수 식이 아니므로 선택적 매개 변수의 기본값으로 배열을 초기화할 수 없습니다. 다음 코드는 BC30059 생성합니다.
' This causes BC30059
Public Function MyFun(Optional filters() As (String, String) = New (String, String)() {}) As Boolean
' Function body
End Function
해결 방법 1: 선택 사항 대신 ParamArray 사용
가변 개수의 인수를 수락해야 하는 경우 선택적 매개 변수 대신 사용하는 ParamArray 것이 좋습니다.
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"))
해결 방법 2: Nothing을 기본값으로 사용하고 메서드 본문에서 초기화
기본값을 설정하여 Nothing 메서드에서 확인합니다.
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")})
해결 방법 3: 매개 변수 없이 오버로드 제공
매개 변수가 필요하지 않은 오버로드된 버전의 메서드를 만듭니다.
' 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
참고하십시오
.NET