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

风流意气都作罢 提交于 2020-06-16 04:01:31

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/35504482/vb-net-two-ways-of-passing-array-as-parameter-are-identical

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!