Or in MsgBox Function

  • Thread starter Thread starter James
  • Start date Start date
J

James

Can someone explain to me what the Or does here?

Dim intReply as Integer = _
MsgBox(strPrompt, MsgBoxStyle.OKCancel Or
MsgBoxStyle.Critical Or MsgBoxStyle.DefaultButton2,
strTitle)

I don't have VB on this cpu... will it display the ok and cancel
buttons and the critical icon ... i still don't understand
DefaultButton, can someone explain that to me as well? Thanks!
 
James said:
Can someone explain to me what the Or does here?

Dim intReply as Integer = _
MsgBox(strPrompt, MsgBoxStyle.OKCancel Or
MsgBoxStyle.Critical Or MsgBoxStyle.DefaultButton2,
strTitle)

I don't have VB on this cpu... will it display the ok and cancel
buttons and the critical icon ... i still don't understand
DefaultButton, can someone explain that to me as well? Thanks!

In this context, Or is the bitwise operator. It allows you to combine
bitfield values. In the way it's being used, it's exactly the same as
using + For example

If OKCancel = 1 and Critical = 2 and DefaultButton2 = 4 then

OKCancel Or Critical Or DefaultButton2 = 7
OKCancel + Critical + DefaultButton2 = 7

However Or is the better way to do it, since it keeps you from adding
the same value twice:

OKCancel + Critical + DefaultButton2 + OKCancel = 8
OKCancel Or Critical Or DefaultButton2 Or OKCancel = 7

Do an Internet search for "Bitwise Operators" and you'll probably find
some better explanations.

DefaultButton determines which button on the Message Box has focus when
it is shown. If you pick DefaultButton2 and OKCancel then Cancel would
be in focus an would be the action if the user pressed the space bar or
the Enter key. DefaultButton1 would be the OK button (left to right).

Adam Ruth
 
so since sgBoxStyle.OKCancel = 1
MsgBoxStyle.Critical = 16
MsgBoxStyle.DefaultButton2 = 256

does that make the bitwise value 273? which is then the value of
intReply? Sorry, I've never delt with this "bitwise" stuff... What is
this used for? Thanks!
 
James said:
so since sgBoxStyle.OKCancel = 1
MsgBoxStyle.Critical = 16
MsgBoxStyle.DefaultButton2 = 256

does that make the bitwise value 273? which is then the value of
intReply?

Correct, the total of those values is 273. intReply in your example
would actually be set to a value of type MsgBoxResult which would
indicate which button the user pressed. 273 is a number that is passed
to the function which tells the function how to display the message box.
Sorry, I've never delt with this "bitwise" stuff... What is
this used for? Thanks!

No problem, it can be a bit confusing when first encountered. Bitfields
are a way to combine several options into one parameter. Instead of
having separate parameters for each of the options, you combine the set
of options into one number with the Or operator. You'll notice that the
values for each of the options are each double the other one, you have
1, 2, 4, 8, 16, 32, 64, 256, etc. This is because each number
represents a bit in a 32-bit number.

Wikipedia has a good description of how bitwise operations work. Think
of the options parameter as a set of 32 on/off switches and each of the
options (that are joined with Or) as turning on one of the 32 options.

http://en.wikipedia.org/wiki/Bitwise

Adam Ruth
 
You might check out the Flags attribute for Enumerations. The Flag attribute
makes the or'ing of different enumerations possible such that the receiving
code, i.e.,, msgbox code, can easily decipher what different values of the
Enumeration are passed.
 
Hi Dennis,
Just last week I looked this stuff up "again" and it appears that the Flags
attribute doesn't really affect the way the bitmask works, just how the
..ToString() function treats the value.

Correct me if I'm wrong...
Steve
 
Back
Top