How to use WithEvents keyword with global variable?

后端 未结 4 514
无人及你
无人及你 2021-01-05 13:24

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

Public WithEvents MyObject As MyClass

The help files say that WithEvent

4条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-05 13:52

    A Case for One Simple Approach

    It is sometimes very useful for a Class or even a UserControl (which is really just a special sort of Class) to have properties and methods of wide general use throughout a program.

    If the main concern is wide use of "utility" methods that are stateless (e.g. someting Web related that has methods for URL encoding, entity encoding, etc.) it can be fairly cheap just to have an additional global instance declared "without" events that you don't cause to load up heavy internal state (arrays, Collections, etc.). Since your program needs all of the code anyway and only one copy sits in memory the second instance can be fairly cheap.

    Another option is to factor those "common" operations and even property values out into a separate Class or static (BAS) module.

    But sometimes you have a fairly complex UserControl or Class that you don't want to customize by refactoring into separate modules or otherwise alter. Maybe you maintain a common, standard versions for use in different projects. Maybe it's a 3rd party library you don't even have the source for. Whatever.

    Something to Consider

    This brings us to another technique that might work for you even though in the immortal words of Irving Mainway it's "not for blind kids." If you are experienced enough to be using this then it should already have occurred to you - but none of us has perfect memory (if indeed we have ever taken the time to Read That Fine Manual).

    So perhaps this won't work for you for some reason and you've already considered and discarded the idea.

    Your container object (Form, UserControl, parent Class, etc.) can carefully set a global reference to the instance of the object in question when it initializes and remove the reference when it terminates. This should be done with some care to avoid circular or orphaned references that can keep your other objects or even your entire program from unloading.

    This should not be taken likely or used carelessly by Johnny GoTo. But if you know what you are doing in VB and have actually read and understood the parts of the manual that discuss this very technique and its pitfalls it can be useful.

    Say you have some Form1 as your program's Startup Object. In Form1 you have an instance of a UserControl "WebWiz" you wrote, named WebWiz1. But elsewhere (another three or twelve Forms?) you want to call a WebWiz method that converts a set of common MIME type string values to file extensions. Only Form1 needs to handle WebWiz1's events, and WebWiz is fairly heavy on internal state so you don't want to create additional instances just for this method.

    In a static module you can simply declare a:

    Public gWebWiz As WebWiz

    When Form1's Initialize or Load events run (or for a Class, after you create the WithEvents instance) you can:

    Set gWebWiz = WebWiz1

    In Form1's Unload event handler:

    Set gWebWiz = Nothing

    Then your program has a global reference it can use for miscelleneous purposes.

    Looks easy right?

    Well if your programs using this are written as a proper "tree" of code starting at a Sub Main or a main Form you generally don't have to exercise much extra caution. But if you are one of those people who code like the Pinball Wizard and find yourself asking questions like "How do I find all of my open Forms and unload them?" then you:

    • Have more work to do in order to avoid programs that "hang" and,
    • Probably aren't really up to using this technique.

提交回复
热议问题