15

in C#:

public string Property { get; private set; }

in VB?

Please vote or/and share your ideas!

MPelletier
  • 16,256
  • 15
  • 86
  • 137
Shimmy Weitzhandler
  • 101,809
  • 122
  • 424
  • 632

3 Answers3

9

Like this:

Private Thingy As Integer
Property Thing() As Integer
    Get
        Return Thingy
    End Get
    Private Set(ByVal value As Integer)
        Thingy = value
    End Set
End Property

Auto property in VB10

Property PartNo As Integer = 44302

But with a private set still can't be done in vb not even in VB10 see here:

From MSDN (as john said):

Property Definitions That Require Standard Syntax :

  • Specify different accessibility for the Get and Set procedure. For example, you might want to make the Set procedure Private and the Get procedure Public.
albertjan
  • 7,739
  • 6
  • 44
  • 74
5

I don't think that is possible (yet).

See this link on MSDN.
The above article even links to another one about mixed access levels.

I found this on Microsoft Connect, so they are thinking about it (If it will be for VS2010 that's another question).

fretje
  • 8,322
  • 2
  • 49
  • 61
  • Would love to hear how the new mixed access level auto-props will look! – Shimmy Weitzhandler Jun 09 '09 at 09:28
  • If you read both articles you'll see that mixed access level auto properties will not be available in VS2010 – albertjan Jun 09 '09 at 10:49
  • In the last link you posted they're talking about a ReadOnly property this is a property 'without' a Set not a Private Set. So still no mixed access level auto properties in vs2010. – albertjan Jun 10 '09 at 06:39
  • Isn't a readonly property in vb equivalent with a private set in C#? You have to be able to set the variable somewhere, don't you? And as we're talking about auto-properties, being properties without a private backing field (or rather "with one that's generated on the fly"). – fretje Jun 10 '09 at 07:15
  • You do have to set the variable somwhere but the equivalent of a readonly property in c# is public string Property { get; } Which is impossible as it will be in vb. In the current vb autoprop syntaxt it is afaics impossible to make a private or internal set. If you read the article at your last post and see the proposed solution you'll see it's as I say. – albertjan Jun 10 '09 at 10:32
  • 1
    are you freakin kidding me? no auto properties in VB? This is another example of why I HATE VB.NET with a passion – PositiveGuy Jun 22 '12 at 14:00
  • @CoffeeAddict: Eurm... auto properties do actually exist in VB (see http://msdn.microsoft.com/en-us/library/dd293589.aspx). We're talking about auto properties with private set here... – fretje Jun 22 '12 at 14:27
5

According to this MSDN article, you can't:

Auto-implemented properties are convenient and support many programming scenarios. However, there are situations in which you cannot use an auto-implemented property and must instead use standard, or expanded, property syntax.

You have to use expanded property-definition syntax if you want to do any one of the following:

[...]

  • Specify different accessibility for the Get and Set procedure. For example, you might want to make the Set procedure Private and the Get procedure Public.
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194