开发者

VB.NET Public Property on a single line

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
   开发者_JAVA百科 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.


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...


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


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜