locked
Visual Basic := Syntax

    Question

  • I am looking some code and I do not understand this symbol

    FunctionName(bVar:= False)
    In the above line what does this symbol :=  refers to 
      

    Thanks in advance

     

     

     

    Thursday, May 18, 2006 1:24 PM

Answers

  • The := syntax allows you to refer to the parameter by name instead of just its ordinal position in the signature.

    In the following example we call the same function foo in 4 different ways.

    1. Ordinal position - matching order with declaration Order
    2. Named Parameters - matching order with declaration Order
    3. Named Parameters - Different order with declaration Order
    4. Ordinal position - Diffent order with declaration Order

    Only the 4th one will fail.

    This comes in handy when you have a lot of optional parameters and only want to specify the ones your a going to use and also it helps in sort of self documenting your code if you have good names.


    Example
    Public Class Form1

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim result As String
            result = Foo("fff", 1) 'Order is relevent
            result = Foo(s1:="fff", h2:=1) 'Using Named Parameters - Order is irrelevent
            result = Foo(h2:=1, s1:="fff") 'Using Named Parameters - Order is irrelevent
            result = Foo(1, "fff") 'Will Fail
        End Sub

        Function Foo(ByVal s1 As String, ByVal h2 As Integer) As String
            Console.WriteLine(s1 & h2.ToString)
        End Function
    End Class

    Thursday, May 18, 2006 2:04 PM

All replies

  • := is a Let operator. That is, it assigns a value to a variable.

    The := operator can be used when calling a method with optional parameters. In order not to assign all values, you can choose which to assign other values than the default value. Hard to explain, but look at this example:

    Call Me.OptionalArguments(bVar:=True, dVar:=Me)

     

        Private Sub OptionalArguments(Optional ByVal aVar As Integer = 1, Optional ByVal bVar As Boolean = False, Optional ByVal cVar As String = "Default", Optional ByVal dVar As Object = Nothing)

     

        End Sub

     

    The method can also be called with

    Call Me.OptionalArguments(, True, , Me)

    to give the same result.

     

    Thursday, May 18, 2006 1:38 PM
  • This refers to a technique called 'named arguments'.

    In VB you can bypass the normal order of the parameters in argument lists by using named arguments (also less accurately, but more commonly, called 'named parameters').

    This technique is not limited to methods with optional parameters.

    e.g.,

    sub test(byval x as integer, byval y as integer)

    can be called via: test(y:=1, x:=2)

    That all said, this technique is not usually considered a good practice.

    David Anton
    www.tangiblesoftwaresolutions.com
    Instant C#: VB to C# converter
    Instant VB: C# to VB converter
    Instant C++: C# to C++ converter and VB to C++ converter
    Instant J#: VB to J# converter
    Clear VB: Cleans up VB.NET code
    Clear C#: Cleans up C# code

    Thursday, May 18, 2006 1:49 PM
  • The := syntax allows you to refer to the parameter by name instead of just its ordinal position in the signature.

    In the following example we call the same function foo in 4 different ways.

    1. Ordinal position - matching order with declaration Order
    2. Named Parameters - matching order with declaration Order
    3. Named Parameters - Different order with declaration Order
    4. Ordinal position - Diffent order with declaration Order

    Only the 4th one will fail.

    This comes in handy when you have a lot of optional parameters and only want to specify the ones your a going to use and also it helps in sort of self documenting your code if you have good names.


    Example
    Public Class Form1

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim result As String
            result = Foo("fff", 1) 'Order is relevent
            result = Foo(s1:="fff", h2:=1) 'Using Named Parameters - Order is irrelevent
            result = Foo(h2:=1, s1:="fff") 'Using Named Parameters - Order is irrelevent
            result = Foo(1, "fff") 'Will Fail
        End Sub

        Function Foo(ByVal s1 As String, ByVal h2 As Integer) As String
            Console.WriteLine(s1 & h2.ToString)
        End Function
    End Class

    Thursday, May 18, 2006 2:04 PM