Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Events Questions 1

Status
Not open for further replies.

bsimbeck

Programmer
May 2, 2003
44
US
I have a class with an event. I am trying to raise the event and have another project in the same solution capture that event. Here is the code:

The class with the event
Code:
Public Class DispatchInfo
    Public Event newDispatch(ByVal dispInfo As DataTable)

    Public Sub recieveDispatch(ByVal disp As DataTable)
        RaiseEvent newDispatch(disp)
    End Sub

End Class

The code that calls the class with event
Code:
Public Sub sendDispatch(ByVal dt As DataTable, ByRef lstlog As ListBox)
        lstLog.Items.Add("Sending data on Call # " & dt.Rows(0)("CallNum"))
        Dim x As New DispatchInfo.DispatchInfo
        x.recieveDispatch(dt)
        lstLog.Items.Add("Send Completed")
        lstLog.Items.Add("Waiting for CAD.......")
    End Sub

The code that captures the event
Code:
Private WithEvents dispObj As New DispatchInfoObject.DispatchInfo

    Private Sub dispObj_newDispatch() Handles dispObj.newDispatch
        If dispInfo.Rows.Count > 0 Then
            Dim row As DataRow = dispInfo.Rows(0)
            Dim dispatchInfoArray As String

            If Not IsDBNull(row(0)) Then
                dispatchInfoArray = row(0) & "|"
            Else
                dispatchInfoArray = " |"
            End If

            If Not IsDBNull(row(1)) Then
                dispatchInfoArray = dispatchInfoArray & row(1) & "|"
            Else
                dispatchInfoArray = dispatchInfoArray & " |"
            End If

            If Not IsDBNull(row(2)) Then
                dispatchInfoArray = dispatchInfoArray & row(2) & "|"
            Else
                dispatchInfoArray = dispatchInfoArray & " |"
            End If

            If Not IsDBNull(row(3)) Then
                dispatchInfoArray = dispatchInfoArray & row(3) & "|"
            Else
                dispatchInfoArray = dispatchInfoArray & " |"
            End If

            If Not IsDBNull(row(4)) Then
                dispatchInfoArray = dispatchInfoArray & row(4) & "|"
            Else
                dispatchInfoArray = dispatchInfoArray & " |"
            End If

            If Not IsDBNull(row(5)) Then
                dispatchInfoArray = dispatchInfoArray & row(5) & "|"
            Else
                dispatchInfoArray = dispatchInfoArray & " |"
            End If

            If Not IsDBNull(row(6)) Then
                dispatchInfoArray = dispatchInfoArray & row(6) & "|"
            Else
                dispatchInfoArray = dispatchInfoArray & " |"
            End If

            If Not IsDBNull(row(7)) Then
                dispatchInfoArray = dispatchInfoArray & row(7) & "|"
            Else
                dispatchInfoArray = dispatchInfoArray & " |"
            End If

            If Not IsDBNull(row(8)) Then
                dispatchInfoArray = dispatchInfoArray & row(8) & "|"
            Else
                dispatchInfoArray = dispatchInfoArray & " |"
            End If

            If Not IsDBNull(row(9)) Then
                dispatchInfoArray = dispatchInfoArray & row(9) & "|"
            Else
                dispatchInfoArray = dispatchInfoArray & " |"
            End If

            If Not IsDBNull(row(10)) Then
                dispatchInfoArray = dispatchInfoArray & row(10) & "|"
            Else
                dispatchInfoArray = dispatchInfoArray & " |"
            End If

            If Not IsDBNull(row(11)) Then
                dispatchInfoArray = dispatchInfoArray & row(11) & "|"
            Else
                dispatchInfoArray = dispatchInfoArray & " |"
            End If

            If Not IsDBNull(row(12)) Then
                dispatchInfoArray = dispatchInfoArray & row(12)
            Else
                dispatchInfoArray = dispatchInfoArray & " "
            End If
            If SocketTCP.Connected Then
                SocketTCP.Send(dispatchInfoArray)
            End If
        End If
    End Sub

Please note that each of these items are in seprate projects in the same solution.

I have stepped throught the project and the RaiseEvents line in the DispatchInfo class is executing, but the event handler in the last project is not, and I don't know why.

Any help is apprecated.

Thanks,

Branden
 
and does it work when they are all in the same project?


Christiaan Baes
Belgium

If you want to get an answer read this FAQ faq796-2540
There's no such thing as a winnable war - Sting
 
Yes actually I added the DispatchInfo class into the class that handles the event and stepped through the solution with the same results. The only thing I haven't tried is to combine all three into one project.

Thanks,

Branden
 
x doesn't have a handle for handling the event - as it is declared locally and you haven't used AddHandler. Either after declaring x use the following:
Code:
AddHandler x.newDispatch, AddressOf dispObj_newDispatch()
in this case x would also have to be declared as WithEvents

or instead don't use x at all and use dispObj to call your subroutine that fires the event.
 
Okay I added that line to the event handling function, but it still didn't catch the event. What is the scope of the an event, can it cross instances of a class? Because I am calling function that raises the event from one instance of the class, and trying to capture the event from another instance of the class. Is there away around it?

Branden
 
Well, you can't capture an event from another instance of the class - and the AddHandler shouldn't be in the event handling function - rather it should be here:
Code:
Public Sub sendDispatch(ByVal dt As DataTable, ByRef lstlog As ListBox)
        lstLog.Items.Add("Sending data on Call # " & dt.Rows(0)("CallNum"))
        Dim x As New DispatchInfo.DispatchInfo
        [b]AddHandler x.newDispatch, AddressOf dispObj_newDispatch()[/b]
        x.recieveDispatch(dt)
        lstLog.Items.Add("Send Completed")
        lstLog.Items.Add("Waiting for CAD.......")
    End Sub

This would allow you handling function to handle the event for instance x.
 
Thanks Borvik,

That was the answer that I couldn't get anywhere I looked.

Thanks again,

Branden
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top