在VB.NET中在公共类中创建事件的正确方法 [英] Proper way of creating events in a public class in VB.NET

查看:248
本文介绍了在VB.NET中在公共类中创建事件的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用了一段时间了,但我在我的课程中创建事件相对较新。在使用事件之前,如果我想创建一个在我的项目中可用的公共类,我将为该类创建一个全局变量,然后在我需要使用该类之前初始化该类:



 Public ClassMFTV As New ClassMachineFunctionsTimerVars 





我有什么试过:



既然我正在使用事件,我发现我不能再使用这种方法了。要使用这些事件,我需要在我的班级声明中包含关键字WithEvents:



 Public WithEvents ClassMFTV As New ClassMachineFunctionsTimerVars 





当我尝试这个时,我收到以下错误:



Handles子句需要WithEvents在包含类型或其基本类型中定义的变量



如果我将类声明移动到我的启动表单,错误消失但我只能访问该形式的类变量。我需要能够在整个项目中读取和更新我班级中的变量。这样做的正确方法是什么,以及能够解雇自定义事件?



这是我班级的一部分。我主要使用该类来检查通过计时器收集的一系列变量的变化。当变量发生变化时,我会触发一个事件来更新GUI,数据库等等。



公共类ClassMachineFunctionsTimerVars 

'引发事件以更新机器功能屏幕

Private _Var826 As Integer = -9999
Private _Var827 As Integer = -9999

公共事件Var826ChangedEvent ()
公共事件Var827ChangedEvent()

公共属性Var826()As Integer
获取
Var826 = _Var826
结束获取
Set( ByVal值As Integer)
如果值<> _Var826然后
_Var826 =价值
RaiseEvent Var826ChangedEvent()
否则
_Var826 =价值
结束如果
结束集
结束物业

公共财产Var827()As Integer
Get
Var827 = _Var827
End Get
Set(ByVal value As Integer)
如果值<> ; _Var827然后
_Var827 =价值
RaiseEvent Var827ChangedEvent()
否则
_Var827 =价值
结束如果
结束集
结束物业

结束等级





这是我处理事件的代码:



 Public Sub Var826ChangedEventFired()Handles ClassMFTV.Var826ChangedEvent 

'更新单选按钮
如果Var826 = 0则
Me.RadioXMasterHole。 Checked = True
FormOperatorCDC.RadioXMasterHole.Checked = True
Else
Me.RadioXSetPoint.Checked = True
FormOperatorCDC.RadioXSetPoint.Checked = True
End if


结束次级


公共子Var827ChangedEventFired()处理ClassMFTV.Var827ChangedEvent

'更新单选按钮
如果Var827 = 0然后
Me.RadioBNo tTilted.Checked = True
FormOperatorCDC.RadioBNotTilted.Checked = True
Else
Me.RadioBTilted.Checked = True
FormOperatorCDC.RadioBTilted.Checked = True
End If

结束子

解决方案

在这里你可以找到一些可以帮到你的东西:

vb.net - 处理静态类的事件 - Stack Overflow [ ^ ]



使用模块中的事件的唯一方法就像Richard Deeming已经在他的评论中描述的那样......


检查:发现检测一系列变量是否已发生变化的情况 [< a href =https://www.codeproject.com/Answers/1171530/Event-that-detects-whether-a-series-of-variables-h#answer2target =_ blanktitle =New Window> ^ ]

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

解决方案

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 ...


Check this: Event that detects whether a series of variables have changed[^]


这篇关于在VB.NET中在公共类中创建事件的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆