Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have been using classes for a while but I am relatively new at creating events in my classes. Prior to using events, If I wanted to make a public class that was available throughout my project, I would create a global variable for the class and then I would initialize the class before I needed to use the class:

Public ClassMFTV As New ClassMachineFunctionsTimerVars


What I have tried:

Now that I am using Events, I found out that I can no longer use this method. To use the events, I need to include the keywords WithEvents in my class declaration:

Public WithEvents ClassMFTV As New ClassMachineFunctionsTimerVars


When I try this, I get the following error:

Handles clause requires a WithEvents variable defined in the containing type or one of its base types

If I move the class declaration to my startup form, the error goes away but then I can only access the class variables in that form. I need to be able to read and update the variables in my class throughout the project. What is the proper way of doing this plus being able to fire custom events?

Here is a portion of my class. I am mainly using the class to check for changes in a series of variables that get collected via a timer. When the variable changes, I trigger an event to update things like the GUI, a database, etc.

Public Class ClassMachineFunctionsTimerVars

    'Raises events to update the machine function screens

    Private _Var826 As Integer = -9999
    Private _Var827 As Integer = -9999
   
    Public Event Var826ChangedEvent()
    Public Event Var827ChangedEvent()
     
    Public Property Var826() As Integer
        Get
            Var826 = _Var826
        End Get
        Set(ByVal value As Integer)
            If value <> _Var826 Then
                _Var826 = value
                RaiseEvent Var826ChangedEvent()
            Else
                _Var826 = value
            End If
        End Set
    End Property

    Public Property Var827() As Integer
        Get
            Var827 = _Var827
        End Get
        Set(ByVal value As Integer)
            If value <> _Var827 Then
                _Var827 = value
                RaiseEvent Var827ChangedEvent()
            Else
                _Var827 = value
            End If
        End Set
    End Property

End Class


Here is my code for handling the events:

Public Sub Var826ChangedEventFired() Handles ClassMFTV.Var826ChangedEvent

        'Update the radio button
        If Var826 = 0 Then
            Me.RadioXMasterHole.Checked = True
            FormOperatorCDC.RadioXMasterHole.Checked = True
        Else
            Me.RadioXSetPoint.Checked = True
            FormOperatorCDC.RadioXSetPoint.Checked = True
        End If


    End Sub


    Public Sub Var827ChangedEventFired() Handles ClassMFTV.Var827ChangedEvent

        'Update the radio button
        If Var827 = 0 Then
            Me.RadioBNotTilted.Checked = True
            FormOperatorCDC.RadioBNotTilted.Checked = True
        Else
            Me.RadioBTilted.Checked = True
            FormOperatorCDC.RadioBTilted.Checked = True
        End If

    End Sub
Posted
Updated 8-Apr-17 8:59am
v2
Comments
CHill60 6-Apr-17 8:52am    
Use the Improve question link to post the code from your class - deosn't have to be all of it but make sure you include the events and any events handlers
Ralf Meier 7-Apr-17 8:14am    
I don't understand your requirement completely ...
But ... your class belongs everytime to the Parent where you instance it.
If you want to have your class global acceesible you should instance it where it is independent from one or the other form.
Do you need more than one instance from your class ? If you only need it one time you perhaps could create it also as a Module ...
theskiguy 7-Apr-17 14:56pm    
I only need to have one instance of the class available, however, I need to have the instance available throughout the project, not just the parent. I don't really think I want to instance the class because the new instance would just be a copy of the original instance. Can you please elaborate on your comment about creating it in a module?
Richard Deeming 7-Apr-17 8:42am    
WithEvents and Handles are just "syntatic sugar". You can achieve the same result by manually adding the event handlers using AddHandler.

How to: Write Event Handlers[^]

Just beware of memory leaks if your event source lives longer than your event subscriber.

Here you can find something which should help you :
vb.net - Handling event of static class - Stack Overflow[^]

The only way to use an Event from a Module is like Richard Deeming allready described in his Comment ...
 
Share this answer
 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900