Question Raise and handle events - how does it work?

davidkeeling

New member
Joined
Aug 29, 2008
Messages
3
Location
Suffolk, UK
Programming Experience
10+
Hi

My question is why the following doesn't work. Tracing it, it throws no errors, goes through the RaiseEvent but does not go through OnCellValueChanged; so I guess the event is raised but not heard. (It's a console application.)

Thanks, David

Code:
Module Module1
 
    Sub Main()
        Dim obj As New Cell
        'Dim obj2 As New UIData
        obj.change()
    End Sub
 
    Public Class Cell
        Public Event CellValueChanged(ByVal NewData As String)
        Public Sub change()
            MsgBox("cell will now simulate a chnage in its value")
            RaiseEvent CellValueChanged("fred")
        End Sub
    End Class
 
    Public Class UIData
        Public WithEvents xCell As New Cell
        Private Sub OnCellValueChanged(ByVal NewCellValue As String) Handles xCell.CellValueChanged
            MsgBox("Msg: " & NewCellValue)
        End Sub
    End Class
End Module
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,334
Location
Norway
Programming Experience
10+
It "doesn't work" because the xcell and obj are two different cells, you don't have a listener for obj.CellValueChanged. Try obj2.xCell.change()

Console application normally doesn't do events, instead threading and thread synchronization is used. Do a search for "console application events" for more research.
 
Last edited:

davidkeeling

New member
Joined
Aug 29, 2008
Messages
3
Location
Suffolk, UK
Programming Experience
10+
John, Thanks alot. It worked over here :) (of course!)

Sorry about the unformatted code sample - I've checked out the editor and can now do it.

I searched the forum and the web for events and console apps, but obviously don't understand something - as the events here work with this example console app. Are events in console apps somehow second class citizens? (In fact I'm only using this console app as an example to get to know events better).

Can you suggest any web or paper reading that will explain the fundamentals of events and handlers - that is, how they work behind the scenes of the program.

Whether or not you can, many thanks for your help.

David
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,334
Location
Norway
Programming Experience
10+
No, but console apps are more focused on doing a task and finish, and not linger a UI and mostly respond to user triggered events like windows apps. Thread synchronization methods and asynchronous callbacks are usually more appropricate in console application because that is what you have to do keep a console app alive anyway.
 
Top Bottom