8

In C# I can do this:

public string myProperty { get; private set; }

This is referred to as an "automatic getter/setter" (from what I've heard). Does VB.NET support these? So far, with my properties, all I can do is this:

Public Property myProperty As String
    Get
        Return String.Empty
    End Get
    Private Set(ByVal value As String)
        somethingElse = value
    End Set
End Property

which is extremely clunky.

So... is there a better way?

qJake
  • 16,821
  • 17
  • 83
  • 135
  • possible duplicate of [VB.net equivalent of C# Property Shorthand?](http://stackoverflow.com/questions/460027/vb-net-equivalent-of-c-property-shorthand) – Rowland Shaw Aug 14 '11 at 15:55
  • Take a look here: [http://stackoverflow.com/questions/460027/vb-net-equivalent-of-c-property-shorthand](http://stackoverflow.com/questions/460027/vb-net-equivalent-of-c-property-shorthand) – icesar Apr 07 '11 at 14:40

2 Answers2

24

Yes.

Public Property MyProperty As String

However, you can only make it ReadOnly in VB 14 (vs 2015) or later.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • Ah, that was my real question... I forgot to specify I meant using different scopes on get vs. set (like `get; private set;` for example), but you answered that anyway, since you can't make it ReadOnly. Thanks! – qJake Apr 07 '11 at 19:31
  • Looks like you can [make it readonly](https://msdn.microsoft.com/en-us/library/dd293589%28v=vs.140%29.aspx) starting with 2015. – starwed Jun 07 '16 at 15:28
2

It does but only from framework 4.0 (2010)

http://weblogs.asp.net/gunnarpeipman/archive/2009/11/01/net-framework-4-0-vb-net-supports-automatic-properties.aspx

pingoo
  • 2,074
  • 14
  • 17