73

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.

RiddlerDev
  • 7,370
  • 5
  • 46
  • 62
  • 3
    Yea, I wish there was a version as concise as C#: `public bool HasValue { get; private set; }` – Jeff B Apr 03 '13 at 16:59
  • Can you also expand the C# declaration if you wish? For example, for setter of the `Name` property in VB.NET you can split it in to `FirstName` and `Surname` if you wish and then set them at the same time. Can C# do that, or is the declaration above as far as you can go? – David Gard Jan 07 '14 at 16:32
  • @DavidGard you can go as far in C# as you can in VB.NET... but can also go a lot shorter what is pretty convenient – Breeze Mar 24 '16 at 08:21

6 Answers6

129

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
  • 12,355
  • 5
  • 41
  • 45
31

I'm not sure what the minimum required version of Visual Studio is, but in VS2015 you can use

Public ReadOnly Property Name As String

It is read-only for public access but can be privately modified using _Name

Breeze
  • 2,010
  • 2
  • 32
  • 43
  • 1
    Yes appears to be a feature that has only been added in Visual Studio 2015 See https://msdn.microsoft.com/en-us/library/dd293589(v=vs.140).aspx - and compare the section "Property Definitions that require Standard Syntax" to the VS 2013 version of the article - and you'll notice that ReadOnly properties require it in VS2013, but not VS2015 – James S Apr 06 '16 at 12:58
  • 2
    The problem with "ReadOnly" is that you can't set the property even from within the class. When you mark the set as protected or private, you can set it within the class, which gives a nice level of control. I wish you could specify two scope, like "Public Private Property Name As string" as a short hand. – Brain2000 May 05 '17 at 03:45
  • 7
    @Brain2000 as mentioned in the answer you can set the property from within the class using an underscore in front of the property name. Sadly intellisense doesn't know about that. – Breeze May 05 '17 at 05:05
  • To add to @Breeze answer and comment... You can also add a Public Method that takes a value and then could set the ReadOnly property using the backing field. – Nathan Mar 11 '20 at 12:55
8
    Public Property Name() As String
        Get
            Return _name
        End Get
        Private Set(ByVal value As String)
            _name = value
        End Set
   End Property
Dan
  • 17,375
  • 3
  • 36
  • 39
6

One additional tweak worth mentioning: I'm not sure if this is a .NET 4.0 or Visual Studio 2010 feature, but if you're using both you don't need to declare the value parameter for the setter/mutator block of code:

Private _name As String

Public Property Name() As String
    Get
        Return _name
    End Get
    Private Set
        _name = value
    End Set
End Property
Mass Dot Net
  • 2,150
  • 9
  • 38
  • 50
5

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

Adam H
  • 561
  • 1
  • 9
  • 20
  • 1
    This should be the updated answer. Its also possible without the backing field https://msdn.microsoft.com/en-us/library/dd293589(v=vs.140).aspx#code-snippet-4 – EvilDr May 04 '17 at 08:35
  • I'm aware the backing field isn't required. I thought id include the backing field for completeness. Updated answer to include example without backing field. Thanks – Adam H May 04 '17 at 09:24
-4

If you are using VS2010 or later it is even easier than that

Public Property Name as String

You get the private properties and Get/Set completely for free!

see this blog post: Scott Gu's Blog

ChrisG
  • 1,316
  • 1
  • 11
  • 14
  • 7
    Yes, but you don't get a private set. The OP would like the equivalent of an auto-implemented `ReadOnly` property that you can set inside the class. You bring up an interesting point though. You may be able to have a `ReadOnly` public property, but use the being the scenes `_Name` private property to set it. But if there's any behavior you want to put in the `Set` that might not work so well. You might mess around with it and expand your answer by editing it :) – Jeff B Jun 24 '14 at 16:27