473,402 Members | 2,046 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

Help with using bits in an integer

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
Oct 2 '06 #1
2 2178

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.PermissionsEnu m.ID)
thePermissions.Name =
DataReader.GetString(DataPermissions.PermissionsEn um.Name)
thePermissions.CanReadCritical =
DataReader.GetBoolean(DataPermissions.PermissionsE num.CanReadCritical)
thePermissions.CanAddIM =
DataReader.GetBoolean(DataPermissions.PermissionsE num.CanAddIM)
thePermissions.CanAddIP =
DataReader.GetBoolean(DataPermissions.PermissionsE num.CanAddIP)
thePermissions.CanAddAP =
DataReader.GetBoolean(DataPermissions.PermissionsE num.CanAddAP)
thePermissions.CanAddLP =
DataReader.GetBoolean(DataPermissions.PermissionsE num.CanAddLP)
thePermissions.CanDeleteIM =
DataReader.GetBoolean(DataPermissions.PermissionsE num.CanDeleteLP)
thePermissions.CanDeleteIP =
DataReader.GetBoolean(DataPermissions.PermissionsE num.CanDeleteIP)
thePermissions.CanDeleteAP =
DataReader.GetBoolean(DataPermissions.PermissionsE num.CanDeleteAP)
....
....
Else

' Failed....

End If
Oct 2 '06 #2
Hello Harry,

Check out the BitConverter class.

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

Oct 3 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jim Hubbard | last post by:
I have some C# code that is supposed to wrap the defrag APIs and I am trying to convert it to VB.Net (2003). But, I keep having problems. The C# code is relatively short, so I'll post it...
8
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- ...
6
by: Clint Olsen | last post by:
I had this crazy idea to use stdarg macros to copy data in a generic fashion, regardless of type. However, I'm not sure it will work in part due to the ANSI C default argument promotions and the...
25
by: bruce.james.lee | last post by:
hi i have a problem with integer subtraction in C. printf("%d", c < (a - b)); a is got from a #define and is 0x80000000 and b is got from input and is also 0x80000000. c is ffffffff (-1). Now,...
13
by: Chua Wen Ching | last post by:
Hi there, I saw this article here in vb.net. http://www.error-bank.com/microsoft.public.dotnet.languages.vb.1/148992_Thread.aspx and ...
0
by: Shaggyh | last post by:
hi im needing help with a program im writing to do subnetting i was on before about it and i got some help. the code below wont work for me and i cant think of why not. i was wondering if anyone...
2
by: Martin Ho | last post by:
Hi Everyone, I have this code of Mersenne twister, which produces the random numbers, one of the fastest codes as far as I know to produce random numbers. Anyways, it's writen in c# and I need to...
3
by: rob | last post by:
Would somebody be kind enough to convert this code to VB.Net for me? private string GetRtfImage(Image _image) { StringBuilder _rtf = null; // Used to store the enhanced metafile...
6
by: ransoma22 | last post by:
I developing an application that receive SMS from a connected GSM handphone, e.g Siemens M55, Nokia 6230,etc through the data cable. The application(VB.NET) will receive the SMS automatically,...
1
by: marsguy85 | last post by:
a program that repeatedly request an integer from the user and displays the integer as a binary number. It should terminate when the value 9999 is entered. A typical run might look like: Enter...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.