5

I am trying to create a basic example of raising an event in vb.net, and I am hoping that by studying it thoroughly I can upgrade the way my system receives data from serial port.

Right now I have a system that receives the incoming data from serial port via timers, the problem is there are certain events in a system that conflicts in my timer. Because of this I am planning to change the way I received the data from serial ports, instead of timer I want to use vb.net raiseevent.

Unfortunately I can't find a simple example on how to use this event, by searching thoroughly I saw the MSDN's post about this topic and it is here. So, how do I use this example? I tried using it below like that

Public Event TimeExpired(ByVal Status As String)
Public Sub RaiseTimeExpiredEvent()
    RaiseEvent TimeExpired("Your time has run out")
    MessageBox.Show(TimeExpired())
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    RaiseTimeExpiredEvent()
End Sub

It's not working working, the error is

Error 1 'Public Event TimeExpired(Status As String)' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event. C:\Users\Cary\Desktop\Projects\Testing\Testing\Testing\Form1.vb 5 25 Testing

Because of that error I tried to do it like this

Class Form1
Public Event TimeExpired(ByVal Status As String)
Public Sub RaiseTimeExpiredEvent()
    RaiseEvent TimeExpired("Your time has run out")
    MessageBox.Show(TimeExpired())

End Sub


Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    RaiseEvent TimeExpired()
End Sub

End Class

But the error states

Error 2 Argument not specified for parameter 'Status' of 'Public Event TimeExpired(Status As String)'. C:\Users\Cary\Desktop\Projects\Testing\Testing\Testing\Form1.vb 11 9 Testing

Cary Bondoc
  • 2,923
  • 4
  • 37
  • 60

1 Answers1

6

Are you using Visual Studio ? If yes, you can try to display the Error List. For that, click View, and Error List.

Class Form1

    Public Event TimeExpired(Status As String)

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        RaiseEvent TimeExpired("Your time has run out")
    End Sub

End Class

To handle the event, you can add this :

Private Sub HandleTimeExpired(Status As String) Handles Me.TimeExpired
    MsgBox(Status)
End Sub

Here is the full code :

Class Form1

    Public Event TimeExpired(Status As String)

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        RaiseEvent TimeExpired("Your time has run out")
    End Sub

    Public Sub OnTimeExpired(Status As String) Handles Me.TimeExpired
        MsgBox(Status)
    End Sub

End Class

Edit :

If you wanna move the raising of event in a module, you can't. You have to add it in a class. See this link.

Example of Class1 (you should rename it...) :

Public Class Class1

    Private Event TimeExpired(Status As String)

    Public Sub OnTimeExpired(Status As String)
        RaiseEvent TimeExpired(Status)
    End Sub

    Private Sub HandleTimeExpired(Status As String) Handles Me.TimeExpired
       MsgBox(Status)
    End Sub

End Class

And to use it, you have to declare it WithEvents in your Form1 :

Public Class Form1

    Dim WithEvents Class1 As New Class1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Class1.OnTimeExpired("Your time has run out")
    End Sub

End Class
Community
  • 1
  • 1
Drarig29
  • 1,902
  • 1
  • 19
  • 42
  • Yes I'm using VS 2013. Well, I'm just learning vb.net. Nothing happens when I clicked the button, I even tried replacing `msgbox` with `console.writeline` – Cary Bondoc Aug 06 '15 at 00:46
  • 1
    @OneFineDay I think your answer is too hard for him, he's learning... ;) – Drarig29 Aug 06 '15 at 00:53
  • 2
    Your answer seems to be so easy to understand for me. This is great and better! – Cary Bondoc Aug 06 '15 at 00:56
  • 1
    I just think that the OneFineDay's Foo class is useless... According to me, my answer is better, but choose what you prefer! ;) – Drarig29 Aug 06 '15 at 01:02
  • You're using the `On*` convention wrongly. Methods like `OnTimeExpired` should **raise** the event and not **handle** the event. The handler should also probably be `Private` and not `Public`. – Enigmativity Aug 06 '15 at 04:32
  • 1
    @Enigmativity you mean the declaration of `OnTimeExpired`? So by making it `HandleTimeExpired` it will become valid? – Cary Bondoc Aug 06 '15 at 06:55
  • @Drarig29, one more thing if it is okay. I am trying to move the raising of event in a module, unfortunately it states `handles in modules must specify a 'withevents' variable`, I tried to do that but another `error` occurs. :( – Cary Bondoc Aug 06 '15 at 08:21
  • 1
    @CaryBondoc - Yes. The `On*` methods are usually `protected` methods to enable descendent classes to be able to raise the event. – Enigmativity Aug 06 '15 at 08:32
  • 1
    @Drarig29, exellent answer and example. Thanks! – Cary Bondoc Aug 07 '15 at 01:17