RaiseEvent not modifying ByRef argument as expected !

Jaafar Tribak

Well-known Member
Joined
Dec 5, 2002
Messages
9,596
Office Version
  1. 2016
Platform
  1. Windows
Hi Dear all.

Strictly speaking RaiseEvent is not a procedure but looks and does the same as one (ie: Executes code) . It also accepts arguments which can be passed ByVal or ByRef.

When an argument is passed ByRef to a Sub/Function , the procedure can modify the argument itself however this doesn't seem to be the case with RaiseEvent. Is this a known bug documented somewhere as i can't find a logic explanation to this problem.

Here is an example :

1-Class named (EventCls)

Code:
Event EventTest(ByRef Cancel As Boolean)

Public Sub RaiseEventTest()
 
    Dim Cancel As Boolean
    
    Cancel = False
    RaiseEvent EventTest(Cancel)
    MsgBox Cancel [COLOR=seagreen]'Should return TRUE as the Cancel arg is passed ByRef[/COLOR]
 
End Sub

2- Class named (EventHandlerCls)

Code:
Dim WithEvents EventHandler As EventCls

Private Sub EventHandler_EventTest(ByRef Cancel As Boolean)
 
    [COLOR=seagreen]'Modify argument[/COLOR]
    Cancel = True
 
End Sub

3- Bas Module (Caller)

Code:
Dim oEvent As EventCls

Sub Main()
 
    Set oEvent = New EventCls
    oEvent.RaiseEventTest
 
End Sub

Hope someone can give me some pointers.

Regards.
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Hi Jafaar

In your example I can't see a linkage between the EventCls module and the EventHandlerCls module. I reworked it like so and got the desired result (the argument passed by the event was changed). Make sense?

1. EventCls remains the same.

2. EventHandlerCls has this code:

Code:
Dim WithEvents EventHandler As EventCls

Sub CallRaiseEvent()

Set EventHandler = New EventCls

EventHandler.RaiseEventTest

End Sub

Private Sub EventHandler_EventTest(Cancel As Boolean)
Cancel = True
End Sub

3. Caller module has this

Code:
Sub Main()
 
    Dim Handler As EventHandlerCls
    
    Set Handler = New EventHandlerCls
    
    Handler.CallRaiseEvent
    
End Sub

DK
 
Upvote 0
As you said no linkage . In fact i missed the obvious by not even creating an instance of the EventHandlerCls Class ! :oops:

Thanks very much DK.
 
Upvote 0

Forum statistics

Threads
1,214,591
Messages
6,120,427
Members
448,961
Latest member
nzskater

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top