5

Is there any way I can put Public Properties on a single line in VB.NET like I can in C#? I get a bunch of errors every time I try to move everything to one line.

C#:

public void Stub{ get { return _stub;} set { _stub = value; } }

VB.NET

Public Property Stub() As String
    Get
        Return _stub
    End Get
    Set(ByVal value As String)
        _stub = value
    End Set
End Property

Thanks

EDIT: I should have clarified, I'm using VB 9.0.

Mark B
  • 1,166
  • 1
  • 19
  • 31
  • [VB.net equivalent of C# Property Shorthand?](http://stackoverflow.com/questions/460027/vb-net-equivalent-of-c-property-shorthand) covers the answer - an equivalent is available as of VB10. – Dan J Sep 23 '11 at 18:06
  • 1
    Yes you can use collan (:) to right multiple lines in a single line. – Emaad Ali Sep 23 '11 at 18:07
  • 1
    @djacobson: Not quite the same, as the OP isn't actually using automatically implemented properties in the code given... – Jon Skeet Sep 23 '11 at 18:07
  • @Jon Skeet The example given doesn't do anything to the field except assign/retrieve its value... That's the only case in which there exists an equivalent *one-line* property syntax between C# and VB, and that's auto-properties. Is it not so? :) – Dan J Sep 23 '11 at 18:21
  • @djacobson: Yes, but that's not equivalent to the code in the question - that's what I'm saying. (I referred to automatically implemented properties in my answer, too.) – Jon Skeet Sep 23 '11 at 18:25
  • the colo has been there for a long time. I think it is even a remainder of VB6. – chrissie1 Sep 23 '11 at 19:18

3 Answers3

13

You can use automatically implemented properties in both VB 10 and C#, both of which will be shorter than the C# you've shown:

public string Stub { get; set; }

Public Property Stub As String

For non-trivial properties it sounds like you could get away with putting everything on one line in VB - but because it's that bit more verbose, I suspect you'd end up with a really long line, harming readability...

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
3

Yes you can

Public Property Stub() As String : Get : Return _stub : End Get : Set(ByVal value As String) :_stub = value : End Set : End Property

and you can even make it shorter and not at all readable ;-)

Public Property Stub() As String:Get:Return _stub:End Get:Set(ByVal value As String):_stub = value:End Set:End Property
chrissie1
  • 5,014
  • 3
  • 26
  • 26
  • Wow, looks bad but at least it's not 8 lines of code! Considering I'm using VB 9.0 this is the best option. Thanks – Mark B Sep 23 '11 at 19:06
  • 1
    Please consider: MAX(Readability) > MIN(Number of Lines). I wouldn't want to maintain code like the above... – Dan J Sep 23 '11 at 19:15
  • it should work in VB9 it has been there since a long time. http://msdn.microsoft.com/en-us/library/865x40k4%28v=VS.71%29.aspx – chrissie1 Sep 23 '11 at 19:17
  • @Chrissie1 - then I must have another problem, because I get the error "Visual Basic 9.0 does not support auto-implemented properties" and other syntax errors (blue lines) – Mark B Sep 23 '11 at 19:22
  • 1
    What version of VS are you using and against which framework are you compiling? – chrissie1 Sep 23 '11 at 19:26
0

It is possible to define a variable on one line, that behaves like a property, if one is willing to use a slightly different syntax, and link to a C# assembly (or play around with IL).

Tested with VS2017 and .Net 4.7.2.

The C# code (currently not available in VB.Net):

public class Propertor<T>
{
    public T payload;
    private Func<Propertor<T>, T> getter;
    private Action<Propertor<T>, T> setter;

    public T this[int n = 0]
    {
        get
        {
            return getter(this);
        }
        set
        {
            setter(this, value);
        }
    }

    public Propertor(Func<T> ctor = null, Func<Propertor<T>, T> getter = null, Action<Propertor<T>, T> setter = null)
    {
        if (ctor != null) payload = ctor();
        this.getter = getter;
        this.setter = setter;
    }
    private Propertor(T el, Func<Propertor<T>, T> getter = null)
    {
        this.getter = getter;
    }
    public static implicit operator T(Propertor<T> el)
    {
        return el.getter != null ? el.getter(el) : el.payload;
    }
    public override string ToString()
    {
        return payload.ToString();
    }
}

Then in your VB program, for example:

Private prop1 As New Propertor(Of String)(ctor:=Function() "prop1", getter:=Function(self) self.payload.ToUpper, setter:=Sub(self, el) self.payload = el + "a")
Private prop2 As New Propertor(Of String)(ctor:=Function() "prop2", getter:=Function(self) self.payload.ToUpper, setter:=Sub(self, el) self.payload = el + "a")
public Sub Main()
    Console.WriteLine("prop1 at start : " & prop1.ToString)
    Dim s1 As String = prop1
    Console.WriteLine("s1 : " & s1)
    Dim s2 As String = prop1()
    Console.WriteLine("s2 : " & s2)
    prop1() = prop1()
    Console.WriteLine("prop1 reassigned : " & prop1.ToString)
    prop1() = prop2()
    Console.WriteLine("prop1 reassigned again : " & prop1.ToString)
    prop1() = "final test"
    Console.WriteLine("prop1 reassigned at end : " & prop1.ToString)
end sub

This results in:

prop1 at start : prop1
s1 : PROP1
s2 : PROP1
prop1 reassigned : PROP1a
prop1 reassigned again : PROP2a
prop1 reassigned at end : final testa
Wolfgang Grinfeld
  • 870
  • 10
  • 11