Performance implications of adding events to classes in VBA

Posted on 2012-09-16
Last Modified: 2012-09-18
As part of several Excel/Access/VBA automation projects I'm currently involved in, I'm creating some generic reusable classes with different functionality.

In some of these it seems convenient to add some events for different state changes. However some of these objects need to handle very large amounts of data very quickly, so I'm trying to optimize performance in every part of the code I'm doing, and this raised the following questions regarding events:

1. What is the performance implication of adding event raisers to procedures? In some procedures RaiseEvent will potentially be called very often, so this is important.

2. If the class is declared without the WithEvents keyword is the RaiseEvent lines then completely ignored and will not have any perfect on performance at all?
Question by:andreas_rafn
    LVL 44

    Accepted Solution

    If the developer/user doesn't code anything in the event, then it is the equivalent of not being called.  The compiler is smart enough to skip.  If there is code in that event, then it is usually viewed as a synchronous event.  I think there might be ways of coding that aren't exactly synchronous, but it gets tricky, and any such events might thwart your rapid response design/performance.

    I don't know about the second part of your question.
    LVL 30

    Assisted Solution

    Calling Raisevent in itself does not really have a siginificant performance implication. The perfomance implicaitons of the event come in how the event handler is programmed i.e. what it does and that is where perfomance is a concern. As aikimark already points out, the Visual Basic .NET compiler assists you by automatically adding a private delegate field and a public registration method whenever you define an event (reference) and the RaiseEvent statement automatically checks to verify if an event handler exists before raising the event. If the event is Nothing, then there’s no event handler and RaiseEvent terminates. If the event is not Nothing, then RaiseEvent triggers the event (reference). For asyncronous events and delegates, I'd refer you to google.

    Part 2: WithEvents keyword is the RaiseEvent lines then completely ignored
    Without the WithEvents keyword, the object will not raise events. It does not mean that RaiseEvents lines are completely ignored, technically, there is probably some logic inserted by the compiler which first checks whether events are enabled and then whether an event handler is actually defined BEFORE the runtime raises the event - the performance cost of doing this is negligible. However, the end results are that in Visual Basic, events are only raised if the object is declared WithEvents (and therefore the event handler will NOT be called, even if it is defined).

    Author Closing Comment

    Very enlightening, thanks for the answers. I'm beginning to realize that putting some time into understanding the way compilation of the VBA code works might make things a lot easier in the future.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    In case Office 2010 has not been deployed in your environment, this article may be quite useful. In our office, we wanted a way to deploy Microsoft Office Professional Plus 2010 through an automated batch file via logon script. This article is docum…
    This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
    The viewer will learn how to make their project stand out over others by learning how to change colors and shapes, add spaces, change directions, and add bullets to their charts.
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now