WhatsUp
WhatsUp

Reputation: 1636

VB.NET, two ways of passing array as parameter are identical?

A question that always confuses me:

in VB.NET, when declare a Function (or Sub) that accepts an array as parameter, one can write either:

Sub func(par as integer())

or:

Sub func(par() as integer).

So, is par as integer() absolutely identical to par() as integer, even if I add various decorations (e.g. Byval) to them?


I googled and found this page on MSDN, which seems to use the second one.

I also tried to write some test functions, which also suggest that there is no difference.

But it will be nice to have a confirmation, or a counterexample. Thanks!


Going one step further, are par as integer()(), par() as integer(), par()() as integer also identical?

Upvotes: 2

Views: 9727

Answers (1)

Hans Passant
Hans Passant

Reputation: 942119

The VB.NET compiler does not care which version you pick, both are equally valid. Just make it consistent so it always looks to the same to the reader of your code. And consider that you'd like it the same way that VS does it so it is completely consistent. Like Object Browser and IntelliSense, they like the par as Integer() flavor.

But do note the problem you'll have when you want to declare the array size directly:

Dim a(3) As Integer         '' Okay, array with 4 elements
Dim a As Integer(3)         '' Not happy, BC30638
Dim b As SomeType(3)        '' Not happy, constructor call requires New

Parentheses in Basic syntax do too much work.

Upvotes: 4

Related Questions