Question

Okay, so I stumbled upon an auto generated private member I wasn't aware of.

I know that if you have a property named e.g. P then the name get_P is reserved for the getter method and the name set_P is reserved for the setter method.

But what I didn't know was that the name _P is also reserved. It seems that this only applies to properties (not ReadOnly / WriteOnly) and fields defiend as WithEvents.

Public Class Test

    Public Property p As Object

    Public WriteOnly Property pW() As Object
        Set(value As Object)
        End Set
    End Property

    Public ReadOnly Property pR() As Object
        Get
        End Get
    End Property

    Public f As Object
    Public WithEvents fWE As Object

    Private _p As Object
    Private _pW As Object
    Private _pR As Object
    Private _f As Object
    Private _fWE As Object

End Class

The above class will produce the following errors:

1) property 'p' implicitly defines '_p', which conflicts with a member of the same name in class 'Test'.

2) WithEvents variable 'fWE' implicitly defines '_fWE', which conflicts with a member of the same name in class 'Test'.

If I remove all the fields named _{name} and return all members (including NonPublic fields) of type Test one can clearly see the auto generated members.

.cctor (Constructor)
.ctor (Constructor)
__ENCAddToList (Method)
__ENCList (Field)
_fWE (Field)  <------------------------------- *2
_p (Field) <---------------------------------- *1
Equals (Method)
f (Field)
Finalize (Method)
fWE (Property)
get_fWE (Method)
get_p (Method)
get_pR (Method)
GetHashCode (Method)
GetType (Method)
MemberwiseClone (Method)
p (Property)
pR (Property)
pW (Property)
set_fWE (Method)
set_p (Method)
set_pW (Method)
ToString (Method)

So does anybody know why these fields are generated and/or their purpose?

Was it helpful?

Solution

It's because you're using auto generated properties. Those fields are automatically generated as the backing fields for the property. See section 9.7.4 of the VB 10 Lanugage Spec:

9.7.4 Automatically Implemented Properties

If a property omits declaration of any accessors, an implementation of the property will be automatically supplied unless the property is declared in an interface or is declared MustOverride. Only read/write properties with no arguments can be automatically implemented; otherwise, a compile-time error occurs.

An automatically implemented property x, even one overriding another property, introduces a private local variable _x with the same type as the property. If there is a collision between the local variable's name and another declaration, a compile-time error will be reported.

The automatically implemented property’s Get accessor returns the value of the local and the property’s Set accessor that sets the value of the local.

For example, the declaration:

Public Property x() As Integer 

is roughly equivalent to:

Private _x As Integer 
Public Property x() As Integer 
    Get 
        Return _x 
    End Get 
    Set (value As Integer) 
        _x = value 
    End Set
End Property
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top