Results 1 to 5 of 5

Thread: Raise and handle events - how does it work?

  1. #1
    davidkeeling is offline VB.NET Forum Newbie
    .NET Framework
    .NET 1.1 (VS 2003)
    Join Date
    Aug 2008
    Location
    Suffolk, UK
    Posts
    3
    Reputation
    0

    Raise and handle events - how does it work?

    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

  2. #2
    JohnH's Avatar
    JohnH is online now VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Dec 2005
    Location
    Norway
    Posts
    14,204
    Reputation
    2369
    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 by JohnH; 08-29-2008 at 8:35 AM. Reason: wrong perception firstly

  3. #3
    davidkeeling is offline VB.NET Forum Newbie
    .NET Framework
    .NET 1.1 (VS 2003)
    Join Date
    Aug 2008
    Location
    Suffolk, UK
    Posts
    3
    Reputation
    0
    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

  4. #4
    JohnH's Avatar
    JohnH is online now VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Dec 2005
    Location
    Norway
    Posts
    14,204
    Reputation
    2369
    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.

  5. #5
    davidkeeling is offline VB.NET Forum Newbie
    .NET Framework
    .NET 1.1 (VS 2003)
    Join Date
    Aug 2008
    Location
    Suffolk, UK
    Posts
    3
    Reputation
    0
    Many thanks

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Harvest time tracking