Results 1 to 4 of 4

Thread: How does WithEvents get applied when declaring multiple objects on a single line

  1. #1

    Thread Starter
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,900

    How does WithEvents get applied when declaring multiple objects on a single line

    I'm writing some C# code that needs to parse some VBA code and identify any objects that have been declared WithEvents. I'm considering what would happen if multiple objects were declared on a single line.

    Given the following VBA:-
    VBA Code:
    1. Dim WithEvents rv1 As AquatorModel.Reservoir, rv2 As AquatorModel.Reservoir
    ... would the WithEvents be applied to rv2 or only to rv1?

    Thanks in advance
    FD
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: How does WithEvents get applied when declaring multiple objects on a single line

    It certainly is applied to both in VB.NET, so I would expect that it is the same in VBA, although that is not guaranteed.

    Do you have a VBA editor available to you? If so, you can check for yourself by writing that code and then checking the navigation bar at the top of the code window. If both variables are included in the Object List then WithEvents applies to both of them. If only the first is listed then WithEvents would apply to only the first. You could also try applying WithEvents to the second and see if it's allowed, which it is not in VB.NET.

  3. #3
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,116

    Re: How does WithEvents get applied when declaring multiple objects on a single line

    Just tested on an older version of Microsoft Office.

    Code:
    Dim WithEvents A As ThingWithEvents, B As ThingWithEvents
    results in only A showing up in the objects dropdown above the code window.

    Code:
    Dim WithEvents A As ThingWithEvents, WithEvents B As ThingWithEvents
    results in both A and B showing up in the objects dropdown above the code window.

    It is entirely possible that newer versions of Office handle this differently, but I can only test on what I have.

  4. #4

    Thread Starter
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,900

    Re: How does WithEvents get applied when declaring multiple objects on a single line

    Great, thanks to you both for that.

    Do you have a VBA editor available to you?
    Yes but what I don't have any more is the expertise to remember how to use it You're instructions should be enough to let me test it myself.

    It's weird. I haven't touched VBA (or VB6 or VB.Net come to think of it) for years so I feel like I'm coding blind here
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width