10

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?

moodymudskipper
  • 46,417
  • 11
  • 121
  • 167
Andy Danger Gagne
  • 2,107
  • 1
  • 17
  • 26
  • @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 01 '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 03 '11 at 08:07

4 Answers4

6

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.

AndyT
  • 145
  • 9
Alex K.
  • 171,639
  • 30
  • 264
  • 288
  • 1
    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 01 '11 at 13:51
  • The posted code doesn't compile... Oh, I see this was pointed out. – dbasnett Jul 01 '11 at 15:26
4

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.

Patrick McDonald
  • 64,141
  • 14
  • 108
  • 120
2

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
cmsjr
  • 56,771
  • 11
  • 70
  • 62
2

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

dbasnett
  • 11,334
  • 2
  • 25
  • 33