What is the: = "operator in (VB) .NET, or what is it good for?

I see it from time to time and want to know what it is. I tried google but it was filtering out characters from search. I have several books that don't link to it either.

FWIW, I remember there was an assignment operator in pascal.

Can anyone point me to MSDN or a similar page?

+11


source to share


3 answers


You can use the: = syntax to assign Sub or Function parameters 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 calls to Form1_Load have them in reverse order, naming each parameter with the: = operator.



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

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

I don't see this used very often, except for the VBA macros created by the Excel macro that uses it a lot .

+27


source


This is really useful when there are several optional parameters - you can see a lot of code calling in office object models - Word, Excel, etc. When you have 40 parameters, of which 37 of them are optional, and you want to set values ​​for parameters 34 and 40, a clearer lot : = than for a function call ("new", "settings", 1 ,,,, ,,,,,,,,,,, 43, 2 ,,, 7)



I wanted to make this comment on JeffK, but I miss the reputation.

+11


source


VB uses this operator to assign attribute values:

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

+4


source







All Articles