Question : Events

I have been working on some entity classes and wanted to incorporate some events and handling.
This is what I have so far, I believe this follows the .net standards for objects to raise an event?

Public Class User

Private _address1 As String

Public Event ObjectChanged As EventHandler

        '''
        ''' This property exposes the Address1 column
        '''

        '''
        '''
        '''
        Public Property Address1() As System.String
            Get
                Return _address1
            End Get
            Set(ByVal value As System.String)
                _address1 = value
                RaiseEvent ObjectChanged(Me, New EventArgs)
            End Set
        End Property

End Class

1. Would I have Method in this class that would handle the ObjectChanged event? It actually gives me a compiller error saying that ObjectChanged has already been declared

2. I wanted to be able to add some useful events for feature programing.


3. I would also like to raise an event when the property has been changed. (From clean to dirty) So I can raise an event and change the enum that handles the state
Like
        Public Sub ObjectChanged(ByVal sender As Object, ByVal e As EventArgs) Handles Me.ObjectChanged

        End Sub

Answer : Events

Handlers *typically* are outside of the class you declare the event in. If you've done any forms programming, then you have interacted with them numerous times. Take, for example, the button class. The button class has an event that is raised when the button is clicked (OnClick). This event is handled in the hosting form's code (button1_OnClick).

This, however, is not to say that you cannot handle an event internally. You can have private events that are not visible outside of the class. You can also attach multiple handlers to a singe event (or attach a single handler to multiple events).

You can add the appropriate events for you object and raise them in the setters of your properties as you have done in your example.
Random Solutions  
 
programming4us programming4us