=和:=之间有什么区别

时间:2022-01-14 12:47:23

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)?

我确信这很简单,但我找不到正确的字符串来获得谷歌搜索结果。在VB.NET中,=(等号)和:=(冒号后跟等号)有什么区别?

3 个解决方案

#1


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

:=运算符用于在VB.Net中按名称传递参数。例如,请使用以下代码

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

=运算符取决于VB.Net中的上下文。它可以是赋值或比较运算符。例如

Dim x = 42 ' Assignment
if x = 36 Then 
 'Comparison above
End if

#2


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#)

#3


= is a comparison AND a set operator, but := is just a set operator.

=是比较AND集合运算符,但是:=只是一个集合运算符。

Compare: If 7 = 7 Then ...

比较:如果7 = 7那么......

Set: Dim myInt As Integer = 7

设置:Dim myInt As Integer = 7

Say you have a custom object called SuperList whose constructor takes a variable called initialCount, then you can do things like:

假设您有一个名为SuperList的自定义对象,其构造函数采用名为initialCount的变量,那么您可以执行以下操作:

Dim myList As New SuperList(initialCount:=10)

It's just sometimes easier to read a constructor when you know what values you're setting, especially if you have a constructor like SomeConstructor(12, 432, True, False, 32)

当你知道你正在设置什么值时,有时读取构造函数会更容易,特别是如果你有像SomeConstructor这样的构造函数(12,432,True,False,32)

It makes more sense to see SomeConstructor(monthsInYear:=12, daysInYear:=432, leapYears:True, leapDays:=False, daysInMonth:=32)

看到SomeConstructor更有意义(monthsInYear:= 12,daysInYear:= 432,leapYears:True,leapDays:= False,daysInMonth:= 32)

There's probably more but this is what I got off the top of my head.

可能还有更多,但这就是我的头脑。

#1


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

:=运算符用于在VB.Net中按名称传递参数。例如,请使用以下代码

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

=运算符取决于VB.Net中的上下文。它可以是赋值或比较运算符。例如

Dim x = 42 ' Assignment
if x = 36 Then 
 'Comparison above
End if

#2


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#)

#3


= is a comparison AND a set operator, but := is just a set operator.

=是比较AND集合运算符,但是:=只是一个集合运算符。

Compare: If 7 = 7 Then ...

比较:如果7 = 7那么......

Set: Dim myInt As Integer = 7

设置:Dim myInt As Integer = 7

Say you have a custom object called SuperList whose constructor takes a variable called initialCount, then you can do things like:

假设您有一个名为SuperList的自定义对象,其构造函数采用名为initialCount的变量,那么您可以执行以下操作:

Dim myList As New SuperList(initialCount:=10)

It's just sometimes easier to read a constructor when you know what values you're setting, especially if you have a constructor like SomeConstructor(12, 432, True, False, 32)

当你知道你正在设置什么值时,有时读取构造函数会更容易,特别是如果你有像SomeConstructor这样的构造函数(12,432,True,False,32)

It makes more sense to see SomeConstructor(monthsInYear:=12, daysInYear:=432, leapYears:True, leapDays:=False, daysInMonth:=32)

看到SomeConstructor更有意义(monthsInYear:= 12,daysInYear:= 432,leapYears:True,leapDays:= False,daysInMonth:= 32)

There's probably more but this is what I got off the top of my head.

可能还有更多,但这就是我的头脑。