Duncan
Duncan

Reputation: 3

VB.NET Object Class Event not working

I'm trying to write some code which will raise an event in one of my objects which is then handled in one of my window forms. It appears to be quite simple but I can't get the code to work; the program creates the person object, sets the person's name, and even raises the event but doesn't handle the event in the form code. I've copied the class code which contained the 'event' and 'raiseevent' into the main program and it still doesn't work. I'm not sure what the problem is but any help would be appreciate.

The code is written in VB.NET using the VS Express 2012 IDE software.

Public Class clsPerson
  Private m_name As String
  Public Event personviewed()

  Public Property name() As String
    Get
      name = m_name
    End Get
    Set(value As String)
      m_name = value
    End Set
  End Property

  Public Sub personviewedmethod()
    RaiseEvent personviewed()
  End Sub
End Class

and

Public Class Form1
  Public WithEvents clsperson1 As clsPerson

  Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim clsperson1 As New clsPerson
    clsperson1.name = "PersonsName"
    clsperson1.personviewedmethod()
  End Sub

  Private Sub personviewed() Handles clsperson1.personviewed
    MessageBox.Show("***Event raised**")
  End Sub
End Class

Upvotes: 0

Views: 3769

Answers (2)

Pow-Ian
Pow-Ian

Reputation: 3635

It would appear that you want to use the object over and over with different buttons. As was mentioned you are re-declaring the person and thus eliminating its handler. In the future if you want to be able to use this class and its events dynamically, you must use AddHandler to attach the event handler to the new instances.

So you get this:

Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
       Dim clsperson1 As New clsPerson() 
       clsperson1.name = "PersonsName"
       AddHanlder clsperson1.personviewed, AddressOf personviewed
       clsperson1.personviewedmethod()
    End Sub

   Private Sub personviewed()
      MessageBox.Show("***Event raised**")
   End Sub
End Class

In declaring it this way each time you click that button a new person is created and its events are handled independent of all other person objects.

Declaring an object with events is a little permanent. You would do this if it was going to be used through the life of the application and you want to be able to use intellisense.

Upvotes: 1

OneFineDay
OneFineDay

Reputation: 9024

When you re-declare your object it is not the same, instead initialize it completely and the handler will work as intended.

Public Class Form1
Public WithEvents clsperson1 As New clsPerson

 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
   'Dim clsperson1 As New clsPerson -do not re-declare the object
   clsperson1.name = "PersonsName"
   clsperson1.personviewedmethod()
 End Sub

 Private Sub personviewed() Handles clsperson1.personviewed
   MessageBox.Show("***Event raised**")
 End Sub
End Class

Upvotes: 1

Related Questions