views:

69

answers:

2

Is there a way to perform this in VB.NET like in the C-Style languages:

struct Thickness
{
    double _Left;
    double _Right;
    double _Top;
    double _Bottom;

    public Thickness(double uniformLength)
    {
        this._Left = this._Right = this._Top = this._Bottom = uniformLength;
    }
}
+3  A: 

As soon as I post this, someone will provide an example of how to do it. But I don't think it is possible. VB.NET treats the single equals in the r-value as a comparison. For example:

  Dim i As Integer
  Dim j As Integer
  i = 5
  j = i = 4
  Debug.Print(j.ToString())
  j = i = 5
  Debug.Print(j.ToString())

The above code prints 0 (false) and -1 (true).

Mark Wilkins
It's not possible. Here's where Lucian, who leads the VB.Net specification, blogs about whether it's worth adding. http://blogs.msdn.com/lucian/archive/2010/02/12/req7-have-separate-syntax-for-assignment-and-comparison.aspx For bonus marks here's Eric Lippert (works on C# compiler) blogging about how confusing it is in C#. http://blogs.msdn.com/ericlippert/archive/2010/02/11/chaining-simple-assignments-is-not-so-simple.aspx
MarkJ
+3  A: 

Expanding on Mark's correct answer

This type of assignment style is not possible in VB.Net. The C# version of the code works because in C# assignment is an expression which produces a value. This is why it can be chained in this manner.

In VB.Net assignment is a statement and not an expression. It produces no value and cannot be changed. In fact if you write the code "a = b" as an expression it will be treated as a value comparison and not an assignment.

Eric's recent blog post on this subject for C#

At a language level assignment is a statement and not an expression.

JaredPar
Great well explained answer.
Shimmy
For info, it is a wishlist item to add support for this: http://blogs.msdn.com/lucian/archive/2010/02/12/req7-have-separate-syntax-for-assignment-and-comparison.aspx
Rowland Shaw