Derek_83327
Derek_83327

Reputation: 23

Passing enum flags to a class or procedure

I would like to pass multiple enum (bitwise) items to a procedure. Not separately but and'd together similar to the Messagebox command. eg:

MsgBoxStyle.Exclamation + MsgBoxStyle.OkCancel

There's plenty of info on the Flagg option etc but not on how to pass as single combined variable

In vs2019 I have setup the enum

<Flags>
Public Enum Options 
    n = 0
    Check = 1
    Print = 2
    Mail = 4    
End 

and my function is

Public Function MyMsg(Text As String, MsgOption As Options) As boolean

all good, and when I attempt to write the call

 MyMsg("Lorem  luctus non", Options.Print + Options.Mail)

I get Overload resolution failed because no accessible 'MyMsg' can be called without a narrowing conversion: 'Public Function MyMsg(Text As String, MsgOption As Options) As Boolean': Argument matching parameter 'MsgOption' narrows from 'Integer' to 'Options'

I don't want to create the function with integer otherwise I lose the intellisense showing enum options, again similar to MessageBox.

I can see the issue (narrows from 'Integer' to 'Options') but cant see how to pass the values.

Can any of you guys/gels help ?

Upvotes: 2

Views: 897

Answers (1)

Precious Uwhubetine
Precious Uwhubetine

Reputation: 3007

Use either of these two methods: Both worked in vs 2019.

MyMsg("Lorem luctus non", Options.Print Or Options.Mail)

Or

MyMsg("Lorem luctus non", CType(Options.Mail + Options.Print + Options.Check, Options))

Upvotes: 4

Related Questions