[This post is part of a series, "wish-list for future versions of VB"]

 

IDEA: Have different syntax for assignment "=" or ":=", versus comparison "==". Currently "=" is used for both. Bill McCarthy writes, "To determine which one it means, you have to look at not just the operands but the context of the entire statement". He points out that it's even more confusing with single-line lambdas:

    Dim x = 5

    Dim f = Function(i) x = i  ' this "=" means an equality-test

    Dim g = Sub(i) x = i  ' this "=" means an assignment

 

This issue will become more pressing if VB decides to add "inline assignment", i.e. an expression which performs an assignment. Here are the tricks that C# can do with inline assignment, and the workarounds that VB is forced to use:

    // C# tricks...

    readonly string p { get { return _p ?? (_p = Expensive()); } }

 

    string x, y, z;

    x = y = z = Expensive();

 

 

    ' VB workarounds...

    ReadOnly Property p As String

        Get

            If _p Is Nothing Then _p = Expensive()

            Return _p

        End Get

    End Property

 

    Dim z = Expensive(), y = z, x = z

 

 

 

Note incidentally that VB already uses := for assignment to named parameters.

 

Provisional evaluation from VB team: The inline assignment examples are not compelling: it's best that each statement have at most one side-effect. Read also Eric Lippert's blog where he explains why C# inline assignment is more confusing that it looks. As for the point that it's hard to read which is which, we agree, but feel this would best be addressed by the IDE as a rendering-issue rather than by changing the syntax of the language.