传递一个常量数组给函数在VB.NET [英] Passing a constant array to a function in VB.NET
问题描述
我知道,你可以很容易地传递数组给一个函数,如code下图为
私人小组SomeFunction(BYVAL PassedArray()作为字符串)
对于我作为整数= 0〜PassedArray.Count - 1
的Debug.WriteLine(PassedArray(I))
下一个
结束小组公用Sub测试()
MYARRAY暗淡作为字符串()= {一些,阵,会员} SomeFunction(MYARRAY)
结束小组
但是,有没有办法通过一个常量数组给函数在VB.NET?
比如在PHP中,你可以写:
函数SomeFunction($数组)
{
为($ I = 0; $ I<计数($数组); $ I ++)
{
回波($数组[$ i]);
}
}功能测试()
{
SomeFunction(阵列(一些,阵,会员)); // Works的PHP
}
所以要重申:有没有办法直接传递一个常量数组VB.NET中的函数吗?是否有这样做的任何好处?我想记忆的几个字节可以幸免。
PS:
SomeFunction({一些,阵,会员})这显然给了一个语法错误
我只是想到这一点并不直接回答这个问题,但也许是另一件事情得到在海报的意图 - ParamArray关键字。如果你控制你调用到函数,这样可以让生活轻松许多。
公共功能的MyFunction(BYVAL的ParamArray数p作为字符串())
'p是这里一个普通阵列
结束功能这是一个有效的电话
MyFunction的(新字符串(){一,B,C,D})原来是这样
MyFunction的(一,B,C,D)
I know that you can easily pass an array to a function, like the code below shows
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
But is there a way to pass a constant array to a function in VB.NET?
For instance in PHP, you could write:
function SomeFunction($array)
{
for($i=0;$i<count($array);$i++)
{
echo($array[$i]);
}
}
function Test()
{
SomeFunction(array("some", "array", "members")); // Works for PHP
}
So to reiterate: Is there a way to pass a constant array directly to a function in VB.NET? Is there any benefit in doing so? I imagine a few bytes of memory could be spared.
PS.:
SomeFunction({"some", "array", "member"}) ' This obviously gives a syntax error
Another thing I just thought of that doesn't directly answer the question, but perhaps gets at the poster's intent - the ParamArray keyword. If you control the function you are calling into, this can make life a whole lot easier.
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")
这篇关于传递一个常量数组给函数在VB.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!