我知道您可以轻松地将数组传递给函数,如下面的代码所示 Private Sub SomeFunction(ByVal PassedArray() As String) For i As Integer = 0 To PassedArray.Count - 1 Debug.WriteLine(PassedArray(i)) NextEnd SubPublic Sub Test()
Private Sub SomeFunction(ByVal PassedArray() As String) For i As Integer = 0 To PassedArray.Count - 1 Debug.WriteLine(PassedArray(i)) Next End Sub Public Sub Test() Dim MyArray As String() = {"some", "array", "members"} SomeFunction(MyArray) End Sub
但有没有办法将常量数组传递给VB.NET中的函数?
例如在PHP中,你可以写:
function SomeFunction($array) { for($i=0;$i<count($array);$i++) { echo($array[$i]); } } function Test() { SomeFunction(array("some", "array", "members")); // Works for PHP }
重申一下:有没有办法将常量数组直接传递给VB.NET中的函数?这样做有什么好处吗?我想可以节省几个字节的内存.
PS:
SomeFunction({"some", "array", "member"}) ' This obviously gives a syntax error我刚才想到的另一件事并没有直接回答这个问题,但可能会得到海报的意图 – ParamArray关键字.如果你控制你正在调用的功能,这可以使生活变得更加容易.
Public Function MyFunction(ByVal ParamArray p as String()) ' p is a normal array in here End Function ' This is a valid call MyFunction(New String() {"a", "b", "c", "d"}) ' So is this MyFunction("a", "b", "c", "d")