如何在vb.net中公开获取属性但是私有设置?

时间:2009-04-25 10:10:12

标签: vb.net

我在一些C#代码中偶然发现了这个...:

public Foo     Foo    { get; private set; }

我怎样才能在vb中做同样的事情?

2 个答案:

答案 0 :(得分:5)

当然(咂了额头)......:

Public Property Foo() As Foo
    Get
        ...
    End Get
    Private Set(ByVal value As Foo)
        ...
    End Set
End Property

我没想到将私人关键字放在那里......

答案 1 :(得分:4)

VB.NET没有像C#3.0那样的自动属性。在VB中,等价物是:


    Private _Foo As SomeType
    Public Property Foo() As SomeType
        Get
            Return _Foo
        End Get
        Private Set(ByVal value As SomeType)
            _Foo = value
        End Set
    End Property