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.

3
  • 3
    Yea, I wish there was a version as concise as C#: public bool HasValue { get; private set; }
    – Jeff B
    Apr 3, 2013 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 7, 2014 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, 2016 at 8:21

6 Answers 6

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
1
  • 4
    For info, this feature was introduced to VB at the same time as v2.0 of the .Net Framework was released. Mar 2, 2010 at 10:21
32

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

4
  • 1
    Yes appears to be a feature that has only been added in Visual Studio 2015 See 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 6, 2016 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 5, 2017 at 3:45
  • 8
    @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 5, 2017 at 5: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, 2020 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
0
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
0
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

2
  • 1
    This should be the updated answer. Its also possible without the backing field msdn.microsoft.com/en-us/library/…
    – EvilDr
    May 4, 2017 at 8: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 4, 2017 at 9: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

1
  • 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, 2014 at 16:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.