Vb.net Property Get and set values ​​without private variables

Greetins

I’m a programmer only for some time, I have some doubts about the basics, could you clarify the following: Case 1:

Public Class BillItems
        Implements INotifyPropertyChanged
        Public Event PropertyChanged As PropertyChangedEventHandler Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
        Private Sub NotifyPropertyChanged(ByVal info As String)
            RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(info))
        End Sub

        Private _BillIdValue As String


        Property BillId As String
            Get
                Return _BillIdValue
            End Get
            Set(ByVal value As String)
                If Not _BillIdValue = value Then
                    _BillIdValue = value
                End If
            End Set
        End Property
End Class

Case 2:

Public Class BillItems
        Implements INotifyPropertyChanged
        Public Event PropertyChanged As PropertyChangedEventHandler Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
        Private Sub NotifyPropertyChanged(ByVal info As String)
            RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(info))
        End Sub


        Property BillId As String
            Get
                Return BillId
            End Get
            Set(ByVal value As String)
                If Not BillId = value Then
                    BillId = value
                End If
            End Set
        End Property
End Case

Does case 1 and case 2 have the same result, I mean that this personal value is necessary there ?, is it possible to use the property itself to use its own value in its Set and get statements?

Thank.

+3
source share
2 answers

I can not imagine that Case 2 will be launched without an exception. You essentially make an endless loop that will constantly call itself.

Case 1 would be the right way to do this.

.Net 4, ( Get/Set):

Property BillId As String

(_BillId) .

Edit:

, :

Property BillId As String
    Get
        Return _BillIdValue
    End Get
    Set(ByVal value As String)
        If Not _BillIdValue = value Then
            _BillIdValue = value
            NotifyPropertyChanged("BillId")
        End If
    End Set
End Property
+4

MSDN , ,

Add code to the Get or Set procedure of a property, such as code to validate incoming values in the Set procedure. For example, you might want to verify that a string that represents a telephone number contains the required number of numerals before setting the property value.

, IPropertyChanged, . - .

Property BillId As String               
    Get       
       Return _BillIdValue               
   End Get               
   Set(ByVal value As String)                   
        If Not _BillIdValue = value Then                       
            _BillIdValue = value                   
            NotifyPropertyChanged("BillID")
        End If               
   End Set           
End Property   

. ( , -)

+2

All Articles