Coding Convention for using Binary FlagWords?

C

Clif McIrvin

Hi all ---

I'm thinking of putting the .Tag property to use by using a binary
("bitwise") encoded flag word. Over time I've come across different
possible uses for .Tag but have never standardized how I use it.

Has someone developed a convention for "parsing" binary flag words? A
simple IF works if I only need to test for one flag, but if I need to
check for the presence of multiple bits the only construct that comes to
mind is a series of IF statements .... something along the lines of:

Enum FlagWord
FlagA = 1
FlagB = 2
FlagC = 4
....
End Enum

Dim vFlags as FlagWord

'validation and error handling omitted for simplicity
vFlags = Me.ctlx.Tag

IF vFlags AND FlagA then
'do something
EndIF
If vFlags AND FlabB then
'do something else
EndIf
If vFlags AND FlabC then
'do a different thing
EndIf
 
D

Dirk Goldgar

Clif McIrvin said:
Hi all ---

I'm thinking of putting the .Tag property to use by using a binary
("bitwise") encoded flag word. Over time I've come across different
possible uses for .Tag but have never standardized how I use it.

Has someone developed a convention for "parsing" binary flag words? A
simple IF works if I only need to test for one flag, but if I need to
check for the presence of multiple bits the only construct that comes to
mind is a series of IF statements .... something along the lines of:

Enum FlagWord
FlagA = 1
FlagB = 2
FlagC = 4
...
End Enum

Dim vFlags as FlagWord

'validation and error handling omitted for simplicity
vFlags = Me.ctlx.Tag

IF vFlags AND FlagA then
'do something
EndIF
If vFlags AND FlabB then
'do something else
EndIf
If vFlags AND FlabC then
'do a different thing
EndIf


I haven't done what you're attempting, but I don't think you can do this:
Dim vFlags as FlagWord

vFlags = Me.ctlx.Tag

You will probably need to store the current flag settings in the Tag
property as the string representation of a number that is the sum or
bitwise-Or of the desired combination of flags. For example, to set the Tag
to the combination of FlagA (1) and FlagC (4), you would do this:

Me.ctlx.Tag = CStr(FlagA + FlagC) ' the Tag is now "5"

If you set up your individual flag values so that each is a different power
of 2, then you can just add them, as above.

To test the flags, you would get the Tag value, convert it to an integer,
and then use the bitwise And operator to see what flags are set. To test
for an individual bit value, you would do it like this:

Dim intFlags As Integer

intFlags = CInt(Me.ctlx.Tag)

' Is FlagA set?
If (intFlags And FlagA) Then
' FlagA was set
End If

To test for a combination of flags, you would do this:

' Are both FlagA and FlagC set?
If (intFlags And (FlagA + FlagC)) = (FlagA + FlagC) Then
' Both flags are set.
End If

I believe that works, but I haven't tested it.
 
C

Clif McIrvin

Dirk Goldgar said:
I haven't done what you're attempting, but I don't think you can do
this:


You will probably need to store the current flag settings in the Tag
property as the string representation of a number that is the sum or
bitwise-Or of the desired combination of flags. For example, to set
the Tag to the combination of FlagA (1) and FlagC (4), you would do
this:

Me.ctlx.Tag = CStr(FlagA + FlagC) ' the Tag is now "5"

If you set up your individual flag values so that each is a different
power of 2, then you can just add them, as above.

To test the flags, you would get the Tag value, convert it to an
integer, and then use the bitwise And operator to see what flags are
set. To test for an individual bit value, you would do it like this:

Dim intFlags As Integer

intFlags = CInt(Me.ctlx.Tag)

' Is FlagA set?
If (intFlags And FlagA) Then
' FlagA was set
End If

To test for a combination of flags, you would do this:

' Are both FlagA and FlagC set?
If (intFlags And (FlagA + FlagC)) = (FlagA + FlagC) Then
' Both flags are set.
End If

I believe that works, but I haven't tested it.

--
Dirk Goldgar, MS Access MVP
Access tips: www.datagnostics.com/tips.html

(please reply to the newsgroup)


Thanks, Dirk.

As always, your reply is detailed and helpful!
I haven't done what you're attempting, but I don't think you can do
this:
I was actually intending to use something like:

if Len(Me.ctlx.Tag)=0 then ---> no flags
if val(Me.ctlx.Tag)=0 then ---> no flags
vFlags = cLng(split(Me.ctlx.Tag)(0)) (if that syntax works)

but abbreviated a lot of the detail on purpose to simplify the question
<grin>.

Thanks again for the reply.

Clif
 
T

Tony Toews [MVP]

Clif McIrvin said:
I'm thinking of putting the .Tag property to use by using a binary
("bitwise") encoded flag word. Over time I've come across different
possible uses for .Tag but have never standardized how I use it.

I just use multiple string values in there and do an Instr to see
what's appropriate. As a developer I want to see at a glance what
that tag is being used for at a glance. That's far more important
that any storage saving.

Tony
--
Tony Toews, Microsoft Access MVP
Tony's Main MS Access pages - http://www.granite.ab.ca/accsmstr.htm
Tony's Microsoft Access Blog - http://msmvps.com/blogs/access/
For a convenient utility to keep your users FEs and other files
updated see http://www.autofeupdater.com/
Granite Fleet Manager http://www.granitefleet.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top