A Microsoft Office (Excel, Word) forum. OfficeFrustration

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » OfficeFrustration forum » Microsoft Access » General Discussion
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

Coding Convention for using Binary FlagWords?



 
 
Thread Tools Display Modes
  #1  
Old May 15th, 2010, 06:55 PM posted to microsoft.public.access
Clif McIrvin[_2_]
external usenet poster
 
Posts: 629
Default Coding Convention for using Binary FlagWords?

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


--
Clif


  #2  
Old May 15th, 2010, 10:34 PM posted to microsoft.public.access
Dirk Goldgar
external usenet poster
 
Posts: 2,529
Default Coding Convention for using Binary FlagWords?

"Clif McIrvin" wrote in message
...
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.

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

(please reply to the newsgroup)

  #3  
Old May 17th, 2010, 04:08 PM posted to microsoft.public.access
Clif McIrvin[_2_]
external usenet poster
 
Posts: 629
Default Coding Convention for using Binary FlagWords?

"Dirk Goldgar" wrote in message
...
"Clif McIrvin" wrote in message
...
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.

--
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:

Dim vFlags as FlagWord

vFlags = Me.ctlx.Tag


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


  #4  
Old May 24th, 2010, 08:26 PM posted to microsoft.public.access
Tony Toews [MVP]
external usenet poster
 
Posts: 3,776
Default Coding Convention for using Binary FlagWords?

"Clif McIrvin" wrote:

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/
 




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT +1. The time now is 09:32 PM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 OfficeFrustration.
The comments are property of their posters.