Re: MsgBox default button




"Timo" <timppa_04@xxxxxxxxxxx> wrote in message
news:ddl271$rl7$1@xxxxxxxxxxxxxxxxxxxxxxxxx

> I do have a bad habit to write "vbYesNo + vbQuestion" but it has
> always worked. I tried quickly reason what difference does it make
> to OR or add those flags. I suppose VB threats them as long anyway?
> Under what conditions adding flags wouldn't work?
> Anyway, I'll take your word and start to use ORing.

Essentially, it's just safer to OR them because you eliminate the risk of
adding the same bitwise value twice. It's just a bad habit that too many
programmers have to add bitwise flags. If you accidently add a bitwise
value twice, you won't get the expected results.

Let's say that for whatever reason, you wrap MsgBox in your own function
(there's probably no practicle reason to do this, I'm just using it for an
example), in which you can pass any of its flags. But in your wrapper
function, you want the question icon only to be used. So, your function
header might look like this:

Public Function MsgBoxWrapper(sPrompt As String, lFlags As VbMsgBoxStyle) As
VbMsgBoxResult

MsgBoxWrapper = MsgBox(sPrompt, lFlags + vbQuestion)

End Function

Now, call this with the following code:

lRet = MsgBoxWrapper("Continue?", vbYesNo + vbQuestion)

Notice that you don't get the question icon, but the information icon. Now,
change the call to this:

lRet = MsgBoxWrapper("Continue?", vbYesNo Or vbQuestion)

Note that you STILL get the information icon. Now, change the line of code
in MsgBoxWrapper to use Or instead of +. You get the question icon as you
want, even though you ORed it twice. Note that for this example to really
be complete, you should remove the flags for the critical, exclamation, and
information icons if any of them happened to get passed to the wrapper
function. If you tried to subtract these values, you'd have a real mess.
Here's what the function should be to ensure, as best as possible, that only
the question icon is displayed:

Public Function MsgBoxWrapper(sPrompt As String, ByVal lFlags As
VbMsgBoxStyle) As VbMsgBoxResult

lFlags = lFlags And Not (vbCritical Or vbInformation Or vbExclamation)
MsgBoxWrapper = MsgBox(sPrompt, lFlags Or vbQuestion)

End Function

Even so, you (or someone) could still butcher the call like this:

lRet = MsgBoxWrapper("Continue?", vbYesNo + vbCritical + vbExclamation +
vbInformation)

You're still not going to get the question icon. However, if you ORed them
you would, despite the fact that all those other icon flags were specified.

As I said, there's probably no practical reason for wrapping MsgBox like
this. I just wanted to use something familiar to show what problems you can
run into when you add bitwise values instead of ORing them. In the case of
MsgBox, it's probably extremely rare that you'd run into problems adding
them because that's just not a mistake that's likely to be made (albeit,
it's still entirely possible).

The point I'm trying to make is that anytime you're dealing with bitwise
values, they should *always* be combined using Or (and likewise, removed
using And Not). It's just the correct way to do it.

--
Mike
Microsoft MVP Visual Basic







.



Relevant Pages

  • Re: MsgBox default button
    ... I tried quickly reason what difference does it make to OR or add those flags. ... Let's say that for whatever reason, you wrap MsgBox in your own function, in which you can pass any of its flags. ... you want the question icon only to be used. ...
    (microsoft.public.vb.controls)
  • Re: large files (> 4GB) ... when will they be ubiquitous?
    ... that enable greater quantities by recompiling with definitions of the ... It's the same reason all code should 'just work' if moved to a ... appropriate flags. ... reasonably-complex portable program already has to deal with this. ...
    (comp.os.linux.development.system)
  • Re: multithreading = sqlserver deadlocks
    ... Here is the deadlock graph of the previous post: ... CleanCnt:1 Mode: U Flags: 0x0 ... > Yes there are other reasons why dead locks occur but the main reason is as I ...
    (microsoft.public.sqlserver.server)
  • Re: the use of #include <a_file.h> v/s #include"a_file.cpp"
    ... the code that you typed will not work for a very simple reason: ... These directories are defined by -I flags on the command ... would need to add a parameter to the compile command line to include ... The reasons for separating the header file from the source are ...
    (comp.lang.cpp)