views:

682

answers:

2

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 properties or a property and a method, just figured this would be cleaner.

+14  A: 

Yes, quite straight forward:

Private _name As String

Public Property Name() As String
    Get
        Return _name
    End Get
    Private Set(ByVal value As String)
        _name = value
    End Set
End Property
JDunkerley
Wow... I code a lot in VB, and had no idea that you could do that.
Meta-Knight
Awesome! I really did not think I could do it, though it seemed like I should be able to. If I could vote up twice I would.
IPX Ares
For info, this feature was introduced to VB at the same time as v2.0 of the .Net Framework was released.
Rowland Shaw
+1  A: 
    Public Property Name() As String
        Get
            Return _name
        End Get
        Private Set(ByVal value As String)
            _name = value
        End Set
   End Property
Dan
You're five minutes late ;-)
Meta-Knight