单行上的 VB.NET 公共属性

发布于 2024-12-06 08:19:36 字数 411 浏览 1 评论 0原文

有什么方法可以像在 C# 中一样将公共属性放在 VB.NET 中的一行上吗?每次我尝试将所有内容移至一行时,都会遇到一堆错误。

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

谢谢

编辑:我应该澄清一下,我正在使用 VB 9.0。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

铃予 2024-12-13 08:19:36

您可以在 VB 10C#,两者都比您显示的 C# 短:

public string Stub { get; set; }

Public Property Stub As String

对于不平凡的属性,听起来您可以将所有内容放在VB中的一行上 - 但因为它有点冗长,我怀疑您最终会得到一条很长的线,从而损害可读性...

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

我最亲爱的 2024-12-13 08:19:36

是的,你可以

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

,你甚至可以让它更短并且完全不可读;-)

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

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
尐偏执 2024-12-13 08:19:36

如果愿意使用稍微不同的语法,并链接到 C# 程序集(或使用 IL),则可以在一行上定义一个变量,其行为类似于属性。

使用 VS2017 和 .Net 4.7.2 进行测试。

C# 代码(目前在 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();
    }
}

然后在您的 VB 程序中,例如:

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

这会导致:

prop1 at start : prop1
s1 : PROP1
s2 : PROP1
prop1 reassigned : PROP1a
prop1 reassigned again : PROP2a
prop1 reassigned at end : final testa

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文