[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

ADO Open Method Options - Bitwise AND

Posted on 2002-03-09
4
Medium Priority
?
231 Views
Last Modified: 2010-05-02
The documentation for the ADO Open method Options parameter states: "Can be one or more CommandTypeEnum or ExecuteOptionEnum values, which can be combined with a bitwise AND operator."

Well, if I combine any two of the enum values using AND, the result is zero.  For example
  dim foo as long
  foo = CommandTypeEnum.adCmdText AND _ ExecuteOptionEnum.adAsyncExecute
  ? foo
  0

This doesn't make sense to me.  Can somebody explain it?

I vaguely recall bit level mainuplation from a C class long ago, can't find anything useful in my VB docs.
0
Comment
Question by:dancebert
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 6853160
Use + instead of AND like this:

CommandTypeEnum.adCmdText + ExecuteOptionEnum.adAsyncExecute
0
 
LVL 17

Expert Comment

by:inthedark
ID: 6853535
Frequently Microsoft will use Enums as swicthes where each switch is either 1 or a power of 2.
i.e.
U 2 4 6 8 16 32 64 128 256 ...etc

You can si the same like:

Private Enum MyOptions
  Option1 = 1
  Option2 = 2
  Option3 = 4
End Type

A long value as 32 bits that can means you can has 32 different switches than can be detected.

So to use these options you can say either:

Dim MyOpt as Long
MyOpt = MyOptions.Option1 + MyOptions.Option2

This will set the bits U and 2 in MyOpt.

The documentation you read was incorrect, infact options like these have to be or'ed together or as emoreau suggests added.

It should have read:

foo = (CommandTypeEnum.adCmdText Or _
            ExecuteOptionEnum.adAsyncExecute)

In C/javascript the or operator is a | symbol.


If you do into imediate window you will see that

?1 or 3
is the same as
?1 + 3

So what Microsoft do in their code is detect that a specifc bit has been set by using the "And" operator

Dim Bit64Set as Boolean

Bit64Set = ((YourOptions And 64) = 64)

Bit64Set will now be either true or false

How this works is:

(YourOptions And 64) will equal 64 or 0

So comparing ot 64 will set tha belean flag.

Hope this helps you understand what's going on....

Forever, inthedark...
0
 
LVL 17

Accepted Solution

by:
inthedark earned 400 total points
ID: 6853543
Same post but with some of the typos removed:

Frequently Microsoft will use Enums as swicthes where each switch is either 1 or a power of 2.
i.e.

U 2 4 6 8 16 32 64 128 256 ...etc

You can do the same like:

Private Enum MyOptions
 Option1 = 1
 Option2 = 2
 Option3 = 4
End Type

A long value has 32 bits.  This means you can have 32 different switches than can be detected.

So to use these options you can say either:

Dim MyOpt as Long
MyOpt = MyOptions.Option1 + MyOptions.Option2

Or you can say:
MyOpt = MyOptions.Option1 Or MyOptions.Option2


This will set the bits U and 2 in MyOpt.

The documentation you read was incorrect.  Infact options like these have to be or'ed together, or as emoreau suggests added using the + operator.

It should have read:

foo = (CommandTypeEnum.adCmdText Or _
           ExecuteOptionEnum.adAsyncExecute)

(In C/Javascript the "Or" operator is a "|" symbol.


If you go into immediate window you will see that

?1 or 2
is the same as
?1 + 2
Both are 3 i.e. Bits U and 2 have been set.

So what Microsoft do in their code is detect that a specifc bit has been set by using the "And" operator

Dim Bit64Set as Boolean

Bit64Set = ((YourOptions And 64) = 64)

Bit64Set will now be either true or false

How this works is:

(YourOptions And 64) will equal 64 or 0

So comparing ot 64 will set tha belean flag.

Hope this helps you understand what's going on....

Forever, inthedark and trying not to speak doubledutch with typos...
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 6853699
the line you posted

 foo = CommandTypeEnum.adCmdText AND _ ExecuteOptionEnum.adAsyncExecute

SHOULD produce 0, as the two values are NOT the same, and do not have a common POWER of 2:

foo = 1 AND 4 will equate to 0

represented in BINARY  

1 = 001
4 = 100

and the bitwise AND of 001 and 100 is 000

Bitwise AND sets the corresponding BIT in the result to 1 if the same bit in BOTH values is 1, and you can clearly see that that is NOT the case with 1 AND 4.

on the other hand, the bitwiose OR operator produces a 1 in the result if the bit is EITHER (or both) numbers is 1:

1 = 001
4 = 100

1 OR 4 = 101 (turning ON the 1 bit and the 4 Bit)

so the OR operator is used to COMBINE binary flags, and the AND operator is used to determine which flags are turned ON.

Arthur Wood
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question