470,590 Members | 1,863 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,590 developers. It's quick & easy.

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

the code was for an overload method
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.
Nov 6 '19 #1
2 2243
269 256MB
":=" isNamed arguments are the function of the method caller

Expand|Select|Wrap|Line Numbers
  1. 'Ex.
  3. Public Function SampleMethod1(Optional x As Integer = 0,
  4.                               Optional y As Integer = 0,
  5.                               Optional z As Integer = 0) As Integer
  6.   Return x + y * 2 + z * 3
  7. End Function
  9. Console.WriteLine("SampleMethod1(1,2,3):{0}", SampleMethod1(1, 2, 3))
  10. ' Output:SampleMethod1(1,2,3)=14
  11. Console.WriteLine("SampleMethod1(1,2,3):{0}", SampleMethod1(x:=1, y:=2, z:=3))
  12. ' Output:SampleMethod1(1,2,3)=14
Dec 18 '19 #2
83 64KB
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.
Expand|Select|Wrap|Line Numbers
  1.     Private Sub DoSomething(Optional a As Integer = 0, Optional b As Integer = 0, Optional c As Integer = 0)
  3.     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...
Expand|Select|Wrap|Line Numbers
  1. 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.
Expand|Select|Wrap|Line Numbers
  1. DoSomething(,, 10)
Dec 18 '19 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Charles Hixson | last post: by
10 posts views Thread by =?Utf-8?B?V2VuZHkgRWxpemFiZXRo?= | last post: by
1 post views Thread by Monica Pnade | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.