14

I see this from time to time and want to know what it is. I did try google, but its filtering out the characters from the search. I have a few books that don't reference it either.

FWIW, I remember in pascal that is was the assignment operator.

Can anybody point me to the MSDN or similar page?

moodymudskipper
  • 46,417
  • 11
  • 121
  • 167
StingyJack
  • 19,041
  • 10
  • 63
  • 122

3 Answers3

29

You can use the := syntax to assign the parameters to a Sub or Function by name, rather than strictly by position. For example:

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        TestRoutine(Y:="TestString", X:=12)
    End Sub

    Private Sub TestRoutine(ByVal X As Long, Optional Y As String = "")
        ' Do something with X and Y here... '
    End Sub

End Class

Note that TestRoutine specifies X as the first parameter, and Y as the second, but the call in Form1_Load has them in the opposite order, naming each parameter with the := operator.

Here's a link to an MSDN article on the subject:

http://msdn.microsoft.com/en-us/library/51wfzyw0.aspx

I don't see this used very often, except in VBA macros generated by Excel's macro recorder, which uses it a lot.

JeffK
  • 3,019
  • 2
  • 26
  • 29
  • A much better answer than mine! – rp. Nov 19 '08 at 17:01
  • Wow - I wish I'd known about that two months ago. That's spectacular. I think some refactoring is in my future... – Electrons_Ahoy Nov 19 '08 at 17:49
  • 4
    It's also extremely useful for calling out what a particular boolean argument means. useStyle:=True is so much clearer to the reader than plain True. – Craig Gidney Nov 05 '09 at 23:53
  • 1
    The `:=` operator is clever and I like the comment from @CraigGidney, but if you're writing methods that need more than a few parameters, they really should be encapsulated in a structure or class. Using APIs with multiple, especially `Optional`, parameters is tricky. Feels like `:=` is a bit of a hack to work around a design problem. – AlainD Jul 27 '21 at 14:06
11

It's really useful when there are multiple optional parameters - you see that a lot in code that's callinginto the office object models - Word, Excel, etc. When you have 40 parameters with 37 of them optional, and you want to set values for parameters 34 and 40, it's a lot clearer to use := than to have a function call that looks like ("new", "settings", 1, ,,,,,,,,,,,,,,,,,,,,,,,,,,,,43,,2,,,,,7)

I wanted to make this a comment to JeffK, but I don't have enough rep.

Richard Gadsden
  • 839
  • 1
  • 6
  • 22
  • 1
    That's pure true horror, did you ever see code like that? God bless monospaced fonts when you have to count commas though. – Camilo Martin Apr 02 '10 at 07:27
  • @Camilo oh yes, if you try programming Office in C#, you end up doing that kind of thing a lot - even something simple like Documents.New() because of all the optional parameters. – Richard Gadsden Apr 06 '10 at 09:57
  • So... C# has no equivalent of VB's ":="? :shocked: – Camilo Martin Apr 06 '10 at 17:43
  • Named parameters have now been added (as ":") in C# 4.0, but that's still in release candidate status as of this writing; it's likely to be released RSN. – Richard Gadsden Apr 07 '10 at 11:07
4

VB uses that operator for attribute value assignments:

http://www.ondotnet.com/pub/a/dotnet/excerpt/vbnut_8/index1.html

rp.
  • 17,483
  • 12
  • 63
  • 79