what does := mean in visual basic .net (console application)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jayxxxx
    New Member
    • Nov 2019
    • 1

    what does := mean in visual basic .net (console application)

    the code was for an overload method
    console.writeli ne(totalprice(p rice:=totalpric e))
    the name of the method was totalprice
    the parameter for it was price

    but I didn't know what does := between the parameter and the function.
  • SioSio
    Contributor
    • Dec 2019
    • 272

    #2
    ":=" isNamed arguments are the function of the method caller

    Code:
    'Ex.
    
    Public Function SampleMethod1(Optional x As Integer = 0,
                                  Optional y As Integer = 0,
                                  Optional z As Integer = 0) As Integer
      Return x + y * 2 + z * 3
    End Function
    
    Console.WriteLine("SampleMethod1(1,2,3):{0}", SampleMethod1(1, 2, 3))
    ' Output:SampleMethod1(1,2,3)=14
    Console.WriteLine("SampleMethod1(1,2,3):{0}", SampleMethod1(x:=1, y:=2, z:=3))
    ' Output:SampleMethod1(1,2,3)=14
    Last edited by gits; Dec 18 '19, 07:01 AM. Reason: use code tags when posting source code!

    Comment

    • IronRazer
      New Member
      • Jan 2013
      • 83

      #3
      Have a look at the msdn link below, it will explain in detail what the := symbol is used for.

      Passing Arguments by Position and by Name

      Basically, it is used to set the value of the arguments in your call to a Sub/Function by their parameter Names instead of setting them in their specified order, separated by commas. You will see the := used with subs and functions that have several Optional parameters.

      For example, this example sub has three Optional parameters, a, b, and c.
      Code:
          Private Sub DoSomething(Optional a As Integer = 0, Optional b As Integer = 0, Optional c As Integer = 0)
      
          End Sub
      You can call this sub by specifying the name of parameter you want to set. Suppose I only want to set the c parameter to a value of 10 when I call it. I can do this...
      Code:
      DoSomething(c:=10)
      On the other hand, you could call this sub and separate each parameter with a comma like below, and get the same result.
      Code:
      DoSomething(,, 10)

      Comment

      Working...