ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Curious use of ByVal keyword in VBA event (https://www.excelbanter.com/excel-programming/276337-curious-use-byval-keyword-vba-event.html)

onedaywhen

Curious use of ByVal keyword in VBA event
 
I have a WithEvents combo object in a class and I'm using its KeyPress
events. Here is the line VBA inserted into my code:

Private Sub m_oCombo_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)

The KeyAscii variable is actually being passed by reference, of
course, otherwise I wouldn't be able to suppress certain characters.
So why the ByVal keyword? It confused me for a while this morning. Is
it this something to do with ByVal being overloaded e.g. for use in
the Declare keyword?

Chip Pearson

Curious use of ByVal keyword in VBA event
 
The MSForms.ReturnInteger is an object type variable, and all objects are
passed by passing the address of the object. The 'ByVal' and 'ByRef'
qualifiers indicate whether the address of the object is passed ByVal or
ByRef. In the KeyPress procedure, the address of KeyAscii is passed ByVal.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
www.cpearson.com






"onedaywhen" wrote in message
m...
I have a WithEvents combo object in a class and I'm using its KeyPress
events. Here is the line VBA inserted into my code:

Private Sub m_oCombo_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)

The KeyAscii variable is actually being passed by reference, of
course, otherwise I wouldn't be able to suppress certain characters.
So why the ByVal keyword? It confused me for a while this morning. Is
it this something to do with ByVal being overloaded e.g. for use in
the Declare keyword?





All times are GMT +1. The time now is 02:08 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com