Specifying default array values ​​in an optional array parameter

Optional parameters require default values, but I cannot assign the default array to an optional array parameter. For instance:

Optional ByVal myArray As Long() = Nothing ' This works

However

Optional ByVal myArray As Long() = New Long() {0, 1} ' This isn't accepted.

The IDE states that New Long() {0, 1}a "constant expression" is required instead .

Is there a trick for assigning an array of constants by default, or is this not allowed?

+4
source share
1 answer

" ", , , . , , . , , .

New . . Nothing :

Sub Foo(Optional ByVal myArray As Long() = Nothing)
    If myArray Is Nothing Then myArray = New Long() {0, 1}
    '' etc...
End Sub
+12

Source: https://habr.com/ru/post/1533171/


All Articles