Help with using bits in an integer

H

Harry Strybos

Hi All

I have a form with about 20 check boxes and I want to persist their state to
a single integer. That is to say, I want to set their Checked property to a
particular bit value contained in an integer (32 possible values)
The following methods (translated from VB6) should help with this?:
Public Sub BitSet(ByVal iValue As Integer, ByVal Bitnum As Integer, ByVal
State As Boolean)

Try

If State Then

iValue = iValue Or CType(2 ^ Bitnum, Integer)

Else

iValue = iValue And Not CType(2 ^ Bitnum, Integer)

End If

Catch : End Try

End Sub

Public Function BitGet(ByVal iValue As Integer, ByVal Bitnum As Integer) As
Boolean

Return CBool(iValue And CType(2 ^ Bitnum, Integer))

End Function

However, there could be some better way to do this in VB.Net (2005). The
whole thing is about user permissions for an inhouse application.

I have had a look at the BitArray class, but find myself somewhat confused
on how to persist this to a database, or read back that value.

If someone could give me some help or a hint, I would be most appreciative.

Thank you
 
R

Robinson

Harry,

Is your database schema already decided? I had a look at a similar system
and I found myself becomming tied up in knots with my stored procedures.
Eventually I went for a table with one bit column for each permission in any
given row. Then I explicitly loaded them into boolean variables from the
database. My enumeration doesn't have to worry about bit manipulation and
neither do my stored procedures. Of course the first idea is always to
store a group of permissions in a single integer, but then what happens if
you need more than 32 or 64 bits of information here? You will need two,
then three, etc. Much better to store this information in a database table
explicitly, because you can mix and match types - if for example instead of
a permissions bit, you want some number (MAX or MIN) or some other data
types.

( Assume an enumeration called PermissionsEnum, that stores the column index
for each item in the set of permissions.
Also assume I have just executed a "GetPermissions ( name )" stored
procedure against the database. Finally, assume
a class called DataPermissions that exposes boolean properties for each
permission ).


If DataReader.HasRows = True Then

DataReader.Read()

thePermissions = New DataPermissions
thePermissions.ID =
DataReader.GetInt32(DataPermissions.PermissionsEnum.ID)
thePermissions.Name =
DataReader.GetString(DataPermissions.PermissionsEnum.Name)
thePermissions.CanReadCritical =
DataReader.GetBoolean(DataPermissions.PermissionsEnum.CanReadCritical)
thePermissions.CanAddIM =
DataReader.GetBoolean(DataPermissions.PermissionsEnum.CanAddIM)
thePermissions.CanAddIP =
DataReader.GetBoolean(DataPermissions.PermissionsEnum.CanAddIP)
thePermissions.CanAddAP =
DataReader.GetBoolean(DataPermissions.PermissionsEnum.CanAddAP)
thePermissions.CanAddLP =
DataReader.GetBoolean(DataPermissions.PermissionsEnum.CanAddLP)
thePermissions.CanDeleteIM =
DataReader.GetBoolean(DataPermissions.PermissionsEnum.CanDeleteLP)
thePermissions.CanDeleteIP =
DataReader.GetBoolean(DataPermissions.PermissionsEnum.CanDeleteIP)
thePermissions.CanDeleteAP =
DataReader.GetBoolean(DataPermissions.PermissionsEnum.CanDeleteAP)
....
....
Else

' Failed....

End If
 

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