Re: Help with using bits in an integer
- From: "Robinson" <itoldyounottospamme@xxxxxxxxxxxxxxxxxxxx>
- Date: Mon, 2 Oct 2006 11:39:55 +0100
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
.
- References:
- Help with using bits in an integer
- From: Harry Strybos
- Help with using bits in an integer
- Prev by Date: turning caps lock off
- Next by Date: CURRENT INDEX of Iteration on ILIST collection ?
- Previous by thread: Help with using bits in an integer
- Next by thread: Re: Help with using bits in an integer
- Index(es):
Relevant Pages
|