[AccessD] VBA Function with parameters

Borge Hansen pcs.accessd at gmail.com
Sun Nov 7 16:05:39 CST 2021


Thanks Paul and Stewart,
Enum is great...
Stewart’s example got me googling …

Link here explains what Enum is in the VBA world:
http://www.cpearson.com/excel/Enums.aspx

With
+++++++++++
Enum CarMakes
   Ford = 1
   Toyota = 2
   Suzuki = 4
End Enum

Function SHowCar(cm As CarMakes) As Long
Dim s As String
If cm And 1 Then s = "Ford "
If cm And 2 Then s = s & "Toyota "
If cm And 4 Then s = s & "Suzuki"
MsgBox s
End Function
++++++++++++

I do understand that if I do
ShowCar(7) I get all three cars in the message box
If I do ShowCar(6)  I get Toyota Suzuki
If I do random number I get for example:
8 - none
9 - Ford
10 - Toyota
ahah! pattern here 9-8=1 - hence Ford
11 - Ford Toyota 11-8 =3 - hence Ford Toyota
12 - Suzuki
13 - Ford Suzuki
and yes
14 - Toyota Suzuki
wait..
15  - Ford Toyota Suzuki
16 - none
etc etc

So what is behind an expression like this?
If cm And x ??
  Bitwise enumeration …

To get my head around the ‘And’ bitwise operator, I found this link which
explains bitwise operators in the C# world:
https://www.alanzucconi.com/2015/07/26/enum-flags-and-bitwise-operators/

To avoid crazy enum parameters - link above - , we can do:

Enum CarMakes
   [_First] = 1
   Ford = 1
   Toyota = 2
   Suzuki = 4
   [_Last] = 4
End Enum

I know most of you are well versed with all of this.
I am posting this thinking it might be helpful for someone else wondering
what is behind the "addition" of enum parameters set up as powers of two.

/borge






On Fri, Nov 5, 2021 at 4:08 PM Stuart McLachlan <stuart at lexacorp.com.pg>
wrote:
>
> Or even:
>


More information about the AccessD mailing list