Question Property structure

JacobSV

New member
Joined
Oct 18, 2013
Messages
2
Programming Experience
Beginner
Hej

My event does not react when one value in the property is change.!
Do i have to make a property for each variable?
Public Class Form1


    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        AddHandler DataOutChanged, AddressOf _DataOutChanged
    End Sub


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        pDataOut.Mode1 = True
    End Sub


    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        pDataOut.Mode2 = True
    End Sub


    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        pDataOut.Mode3 = True
    End Sub


    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        pDataOut.Mode4 = True
    End Sub


    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        pDataOut.Mode1 = False
        pDataOut.Mode2 = False
        pDataOut.Mode3 = False
        pDataOut.Mode4 = False
    End Sub


    Private Sub _DataOutChanged()
        MsgBox("Data changed" & vbCrLf & pDataOut.Mode1.ToString & vbCrLf & pDataOut.Mode2.ToString & vbCrLf & pDataOut.Mode3.ToString & vbCrLf & pDataOut.Mode4.ToString)
    End Sub


    Private Event DataOutChanged()


    Private DataOut As _DataOut = New _DataOut


    Public Property pDataOut() As _DataOut
        Get
            Return DataOut
        End Get
        Set(ByVal value As _DataOut)
            DataOut = value

RaiseEvent DataOutChanged()
        End Set


    End Property
End Class


Public Class _DataOut
    Public Mode1 As Boolean
    Public Mode2 As Boolean
    Public Mode3 As Boolean
    Public Mode4 As Boolean
End Class
 
Last edited:
You're not raising the event anywhere so the event handler is never going to be executed. I suggest that you follow the Blog post in my signature and check out my post on Custom Events.
 
Hi,

If I were doing this then I would implement the INotifyPropertyChanged Interface into your Class and then use the default Event created from that structure to notify your Routine that you have changed something in your class.

To do this create a Class which implements the INotifyPropertyChanged Interface. i.e:-

Public Class SomeClass
  Implements INotifyPropertyChanged
  Public Event PropertyChanged(sender As Object, e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
 
  Private _Name As String
 
  Public Property Name As String
    Get
      Return _Name
    End Get
    Set(value As String)
      _Name = value
      RaiseEvent PropertyChanged(Name, New PropertyChangedEventArgs("Name"))
    End Set
  End Property
End Class


You can than add a Sub to your project which will do what you need when the PropertyChanged Event is raised. i.e:-

Private Sub myCustomer_PropertyChanged(sender As Object, e As System.ComponentModel.PropertyChangedEventArgs)
  MsgBox("The Property that was Changed was " & e.PropertyName)
End Sub


And then finally, create a variable of your class type and add an Event Handler to handle the PropertyChanged event. i.e:-

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
  Dim myCustomer As New SomeClass
  AddHandler myCustomer.PropertyChanged, AddressOf myCustomer_PropertyChanged
 
  myCustomer.Name = "My Company Name"
End Sub


Hope that helps.

Cheers,

Ian
 
You are raising an event when a value is assigned to the 'pDataOut' property of the form. If you're expecting to handle an event when a value is assigned to Mode1, Mode2, Mode3 or Mode4 then you have to raise an event when a value is assigned to them. Again I direct you to my Custom Events blog post. Those four fields need to be re-implemented as properties. One of the reasons that properties exist is so that you can raise an event when their value changes. You will need one event per property, e.g. Mode1Changed for Mode1, or else you can implement the INotifyPropertyChanged interface and use one event for all properties.

Also, make sure that you only raise an event when the property value changes, which you're not doing now and my blog post addresses. Just because a property is set does not mean that its value changes.
 
Hi,

If I were doing this then I would implement the INotifyPropertyChanged Interface into your Class and then use the default Event created from that structure to notify your Routine that you have changed something in your class.

To do this create a Class which implements the INotifyPropertyChanged Interface. i.e:-

Public Class SomeClass
  Implements INotifyPropertyChanged
  Public Event PropertyChanged(sender As Object, e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
 
  Private _Name As String
 
  Public Property Name As String
    Get
      Return _Name
    End Get
    Set(value As String)
      _Name = value
      RaiseEvent PropertyChanged(Name, New PropertyChangedEventArgs("Name"))
    End Set
  End Property
End Class


You can than add a Sub to your project which will do what you need when the PropertyChanged Event is raised. i.e:-

Private Sub myCustomer_PropertyChanged(sender As Object, e As System.ComponentModel.PropertyChangedEventArgs)
  MsgBox("The Property that was Changed was " & e.PropertyName)
End Sub


And then finally, create a variable of your class type and add an Event Handler to handle the PropertyChanged event. i.e:-

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
  Dim myCustomer As New SomeClass
  AddHandler myCustomer.PropertyChanged, AddressOf myCustomer_PropertyChanged
 
  myCustomer.Name = "My Company Name"
End Sub


Hope that helps.

Cheers,

Ian

I don't especially like the INotifyPropertyChanged interface for general use. I prefer a specific event for each property. I only use INotifyPropertyChanged when it's being handled by the system, i.e. for data-binding. That's because you have to test for the property being changed by name. With individual events, you know which property changed for each event without having to check.
 
Hi jmcilhinney,

I only use INotifyPropertyChanged when it's being handled by the system, i.e. for data-binding.

On that point I totally agree with you.

That's because you have to test for the property being changed by name. With individual events, you know which property changed for each event without having to check.

Ok, Yeah, I see where you have gone with that one, but my only Counter to that Comment would be to say that you can have a generic routine that can interpret and deal with any property that was changed within a class.

I suppose it all depends on what the OP is wanting to achieve but all points made are valid.

Cheers,

Ian
 
Hi jmcilhinney,



On that point I totally agree with you.



Ok, Yeah, I see where you have gone with that one, but my only Counter to that Comment would be to say that you can have a generic routine that can interpret and deal with any property that was changed within a class.

I suppose it all depends on what the OP is wanting to achieve but all points made are valid.

Cheers,

Ian

Certainly nothing wrong with using INotifyPropertyChanged. Just a personal preference.
 
Back
Top