:= Operator Help

This might seem kind of stupid, but what does the := operator do


Answer this question

:= Operator Help

  • Kent Thomas

    In addition, Named Arguments make code maintainability easier as you don't have to remember what the following signature does:

    DoWork(myObj, True, True, "Name", Nothing)

    With Named Arguments, you can re-write it as:

    DoWork(source:=myObj, sortAscending:=True, caseSensitive:=True, sortProperty:="Name", sortArgs:=Nothing)

    The second version is much more maintainable for code readers who need to make changes.

    As for the performance concerns, it may require a bit more processor time to compile the source, but the compiled code uses direct assignments and not the named arguments. YMMV if you are using the Named Arguments as part of the anonymous types and object initializers with LINQ as it may use some reflection. Then again, are the ticks you loose through reflection compensated by the maintainability benefits of using the named arguments It depends on your situation.

    Jim Wooley
    http://devauthority.com/blogs/jwooley



  • melberti

    Named arguments come in especially handy if you have optional parameters, rather than having to put in values or lots of commas to denote the different fields, you can simply name your parameters and call them using the := operator.


  • Patrick.I

     

    No it's not stupid. Those as "Named Arguments". Normally arguments are position sensitive, using named arguments are not.

    Here's a brief example from Help.

    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
    

    Call studentInfo(age:=19, birth:=#9/21/1981#, name:="Mary")
    


  • Juan Carlos Trimiño

    Spotty,

    I understand there are performance penalties with named arguments



  • avatar4938

    I would find it strange if there was a performance issue with named arguments.

    One could easily test this, but I think a good rule of thumb to use is to ask yourself "is this something the compiler can sort out ". If so, then there's unlikely to be a performance issue.

    David Anton
    www.tangiblesoftwaresolutions.com
    Instant C#: VB to C# converter
    Instant VB: C# to VB converter
    Instant C++: C# to C++ converter, VB to C++ converter
    Instant Python: VB to Python converter



  • := Operator Help