如何使用WithEvents关键字与全局变量? [英] How to use WithEvents keyword with global variable?

查看:341
本文介绍了如何使用WithEvents关键字与全局变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在VB6模块中声明一个变量,如下所示:

I am trying to declare a variable in a VB6 module as follows:

Public WithEvents MyObject As MyClass

帮助文件说, WithEvents 只能在类模块。为什么不能在 .bas 模块中使用?

The help files say that WithEvents can only be used in class modules. Why can't it be used in .bas modules?

我工作的旧代码有一个对象声明全局模块。我想添加 WithEvents 到这个声明,但我需要保持对象的全局,因为许多其他形式等引用对象。

The legacy code I am working has an object declared globally in a module. I want to add WithEvents to this declaration but I need to keep the object global because many other forms etc refer to the object. How can I achieve this with minimal disruption to the code?

推荐答案

编写一个接受你的全局对象作为参数的接收器其事件。

Write a class that accepts your global object as a parameter and sinks its events.

' Class MySink
Private WithEvents m_oSink As MyClass

Friend Sub frInit(oSink As MyClass)
    Set m_oSink = oSink
End Sub

Private Sub m_oSink_MyEvent()
    '--- implement event
End Sub

.bas 中创建此类的实例模块。

Create an instance of this class in your .bas module.

Public g_oMyObject AS MyClass
Private m_oMySink As MySink

Sub Main()
    Set g_oMyObject = New MyClass
    Set m_oMySink = New MySink
    m_oMySink.frInit g_oMyObject
End Sub

这篇关于如何使用WithEvents关键字与全局变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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