Take the tour ×

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?

share|improve this question
@will what search terms did you use? SO.com doesnt like me putting := into the search and i didnt get much with "colon equals vb.net either"... –  Andy Danger Gagne Jul 1 '11 at 13:46
Andy - that other question appears in the 'related' column at the right - I don't know what system SO uses to populate that list, or whether it would appears in the suggestions you will have been shown when you entered your question. –  Will Dean Jul 3 '11 at 8:07
add comment

marked as duplicate by Will Dean, Magnus, Josh Lee, yoda, Graviton Jul 2 '11 at 7:45


4 Answers

up vote 1 down vote accepted

It strongly names arguments;

function foo(x As Long, y As Long)
   debug.print x, y
end Function

Called with revered but names arguments:

foo y:=999, x:=111


111, 999
share|improve this answer
Your code wouldn't compile. Replace function with sub, debug.print x, y with Debug.Print(String.Format("{0}, {1}", x.ToString, y.ToString)), foo y:=999, x:=111 with ` foo(y:=999, x:=111)` and you get the result. –  Tim Schmelter Jul 1 '11 at 13:51
The posted code doesn't compile... Oh, I see this was pointed out. –  dbasnett Jul 1 '11 at 15:26
add comment

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.

share|improve this answer
Agreed that this is only really useful for functions with a lot of optional args. –  dbasnett Jul 1 '11 at 15:27
add comment

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.

share|improve this answer
add comment

Assigns values by names instead of position.


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

share|improve this answer
add comment
