本文介绍了VB.NET,将数组作为参数传递的两种方式相同吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个总是使我困惑的问题:

在声明 Function (或 Sub )接受一个数组作为参数,可以写以下任何一个:



Sub func(作为整数())



或:



sub func(par()作为整数)



因此, par作为整数() par()作为整数绝对相同,即使我向其中添加了各种装饰(例如 Byval )他们?






我用Google搜索并找到了在MSDN上,该页面似乎使用了第二个页面。



我也尝试编写一些测试函数,这也表明没有什么区别。



但是有一个确认或一个反例将很高兴。






再走一步, par为integer()() par()作为整数() par()()作为整数也相同吗?

解决方案

VB.NET编译器不在乎您选择哪个版本,两者都同样有效。只需使其一致即可,以便代码阅读者始终看起来相同。并且考虑到您希望它与VS一样,因此它是完全一致的。像对象浏览器和IntelliSense一样,它们喜欢 par作为Integer()的味道。



但是请注意问题所在当您想直接声明数组大小时,将具有以下内容:

  Dim a(3)As Integer''OK,array with 4个元素
Dim a As Integer(3)''不满意,BC30638
Dim b As SomeType(3)''不满意,构造函数调用需要New

基本语法中的括号做得太多。


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?

解决方案

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.

这篇关于VB.NET,将数组作为参数传递的两种方式相同吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 02:56