[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.vb.general.discussion

Don't overlook the value of Properties...

Larry Serflaten

1/15/2012 9:13:00 AM

So I'm reading posts from last year... (somewhat bored again...)

I find a guy wants to use bit flags to condense True and False
conditions
of some table and is going to use a string to hold the bits. That's
strike
one, but moving on, he's got his string set up and has routines like:
BitSet(), BitReset(), BitFlip() (etc.) to handle the different things
he may
need to do.

That's a bit cumbersome, using discrete routines to do what VB does
on its own.

Not only do Properties provide for shadowing and input validation, et
al.
but they also have access to both sides of the Equals sign. That
means
you can use just one name for both Let and Get (strongly typed)
access.

The following code can be used in a module, a form, or a class as may
be needed, any time bit flags are needed. In this case I've used a
form:

LFS


Private BitFlags() As Byte
Private BitMask(0 To 7) As Byte


Private Sub Form_Load()
InitBitFlagging
' Once initialized, its all set to use:
Bit(3) = True ' Easy to program
Bit(19) = Bit(1) Xor Bit(3) ' Uses VB Logic ops
MsgBox Bit(19) ' True Automatic resize
MsgBox Bit(1024) ' False Won't resize if not needed
End Sub

Private Sub InitBitFlagging()
Dim i As Long
ReDim BitFlags(0 To 0) As Byte
For i = 0 To 7
BitMask(7 - i) = 2 ^ i
Next
End Sub

Public Property Get Bit(ByVal Index As Long) As Boolean
Dim byt As Long
' Get bit at Index
byt = Index \ 8
If byt <= UBound(BitFlags) Then
Bit = BitFlags(byt) And BitMask(Index And 7)
End If
End Property

Public Property Let Bit(ByVal Index As Long, ByVal Value As Boolean)
Dim byt As Long
' Set bit at Index
If Index < 0 Then Exit Property ' Or throw an out of range error...
byt = Index \ 8
If UBound(BitFlags) < byt Then
ReDim Preserve BitFlags(0 To byt)
End If
If Value Then
BitFlags(byt) = BitFlags(byt) Or BitMask(Index And 7)
Else
BitFlags(byt) = BitFlags(byt) And Not BitMask(Index And 7)
End If
End Property