VB.Net Properties - Public Get, Private Set

后端 未结 6 1816
借酒劲吻你
借酒劲吻你 2021-01-31 13:32

I figured I would ask... but is there a way to have the Get part of a property available as public, but keep the set as private?

Otherwise I am thinking I need two prope

6条回答
  •  既然无缘
    2021-01-31 14:04

    I find marking the property as readonly cleaner than the above answers. I believe vb14 is required.

    Private _Name As String
    
    Public ReadOnly Property Name() As String
        Get
            Return _Name
        End Get
    End Property
    

    This can be condensed to

    Public ReadOnly Property Name As String
    

    https://msdn.microsoft.com/en-us/library/dd293589.aspx?f=255&MSPPError=-2147217396

提交回复
热议问题