Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between = and :=

Tags:

.net

vb.net

I'm sure this is straightforward but I cannot find the correct string to get a google result. In VB.NET what is the difference between = (equals sign) and := (colon followed by equals sign)?

like image 662
Jason Irwin Avatar asked Apr 18 '09 15:04

Jason Irwin


People also ask

What does := mean in go?

The := syntax is shorthand for declaring and initializing a variable, example f := "car" is the short form of var f string = "car" The short variable declaration operator( := ) can only be used for declaring local variables.

What is := in make?

Variables defined with ' := ' or ' ::= ' are simply expanded variables; these definitions can contain variable references which will be expanded before the definition is made.

What does := mean in C?

:= is not a valid operator in C. It does however have use in other languages, for example ALGOL 68. Basically, for what you want to know, the := in this example is used to assign the variable PTW32_TRUE to localPty->wNodeptr->spin.

What is the difference between <> and !=?

No difference. <> is sql standard, != non-standard.

What is === called?

The strict equality operator ( === ) checks whether its two operands are equal, returning a Boolean result. Unlike the equality operator, the strict equality operator always considers operands of different types to be different.

What is the difference between != and =! In C?

A!= B means " A is not equal to B ". A=! B means "Assign the complement of B to A , and yield the lvalue of A ".


2 Answers

The := operator is used to pass arguments by name in VB.Net. For instance take the following code

Sub Foo(p1 As integer, p2 As String)
  .. 
End Sub

Sub Test()
  Foo(p2:="foo",p1:=42)
End Sub

If you look strictly at the types involved here I've passed the values out of order. But Because I bound the arguments by name using :=, the compiler will properly pass the values.

The = operator depends on the context in VB.Net. It can be either an assignment or comparison operator. For instance

Dim x = 42 ' Assignment
if x = 36 Then 
 'Comparison above
End if
like image 64
JaredPar Avatar answered Oct 24 '22 19:10

JaredPar


The equal sign is used for assignment and is also a comparison operator. An example of assignment is

  a = 5

An example of comparison is

  if (a = 5) then
    ' do something here
  end if

The := is used specifically for calling functions with setting particular parameters to the value by name. For example:

Sub studentInfo(ByVal name As String, _
       Optional ByVal age As Short = 0, _
       Optional ByVal birth As Date = #1/1/2000#)

  Debug.WriteLine("Name = " & name & _
                "; age = " & CStr(age) & _
                "; birth date = " & CStr(birth))
End Sub

Normally, you would call the function like this:

Call studentInfo("Mary", 19, #9/21/1981#)

But you can also call the function this way:

Call studentInfo("Mary", birth:=#9/21/1981#)
like image 42
Tommy Hui Avatar answered Oct 24 '22 18:10

Tommy Hui