What does a := (colon equals) in VB.NET do? [duplicate]

ぐ巨炮叔叔 提交于 2019-12-22 04:09:35

问题


Possible Duplicate:
What is the use of the := syntax?

I've tried hunting down the MDSN documentation for := in VB.NET as well as scoured Google only to be linked to a dead MSDN page... What would the purpose of := be?


回答1:


It strongly names arguments, allowing you to call a method with arguments in an order other than that specified in the method definition.

For example:

sub foo (byval x As Long, byval y As Long)
   debug.print (String.Format("{0}, {1}", x.ToString, y.ToString))
end Function

can be called with the order of the arguments reversed by using their names:

foo (y:=999, x:=111)

prints:

111, 999

This is especially useful when you have a long list of optional arguments, you only want to specify a few of them, and those that you want to specify are not the first ones.




回答2:


It's used to name arguments in a method call and is usually used with optional arguments.

It's especially useful for calling Word or Excel methods through ActiveX calls, where there are an awful lot of optional arguments, most of which are never used.




回答3:


Assigns values by names instead of position.

Given

Private Function foo(arg1 As Integer, arg2 As Integer) As Boolean
    Debug.WriteLine("{0}  {1}", arg1, arg2)
    Return True
End Function

these produce the same result

    foo(arg2:=2, arg1:=1)

    foo(1, 2)

debug output

1 2

1 2




回答4:


I am not sure about VB.NET, but in Visual Basic 6.0 that was the syntax for assigning a value to method parameter by name rather than by ordinal position.



来源:https://stackoverflow.com/questions/6548723/what-does-a-colon-equals-in-vb-net-do

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