vb.net - Specifying default array values in an optional array parameter -


optional parameters require default values, yet can't seem assign default array optional array parameter. example:

optional byval myarray long() = nothing ' works 

however

optional byval myarray long() = new long() {0, 1} ' isn't accepted. 

the ide tells me "constant expression required" in place of new long() {0, 1}.

is there trick assigning default array of constants, or not allowed?

it not "constant expression", expression can entirely evaluated @ compile time , produces single simple value can stored in assembly metadata. used, later, in other code makes call. optional values retrieved @ compile time, not runtime, strictly compiler feature.

the new operator must executed @ runtime , requires code so. , therefore not constant expression. simple workaround use nothing , put code in method body:

sub foo(optional byval myarray long() = nothing)     if myarray nothing myarray = new long() {0, 1}     '' etc... end sub 

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -