Stupid question regarding use of Int or UInt

  • Thread starter Thread starter Ender
  • Start date Start date
E

Ender

Let's assume you are developing a database application and you have an
ID field. You setup the ID field in the database as a 1 up increment
starting at zero.

Whenever I see programs, everyone always uses the int field to
represent the userID. Should this really be a uint since the field
will never be negative, only positive? Is there a performance penalty
using the uint type?

Furthermore, let's say I am building an application where I have
numbers that will only be positive. Should uint be user here? Again,
I almost always see int used, even if the business rules state the
number will never be negative.

Thanks

Endymion Keats
 
Never really thought about it, but there is probably a slight overhead
involved with UInt. Int is a - dare I say - natural data type and
there is a lot of performance tweaking to make it work well. UInt
would probably need to be converted at some point.

I doubt there would be much difference either way, and you could use a
UInt with much higher numbers than an INT. Go for it.

Dan
 
I'm not sure the exact reason, but I guess it could be a leftover of "old
habits" from VB6 days as it didn't support UInts (which would have been
ULongs in VB6 anyway).
Is there a performance penalty
using the uint type?

There shouldn't be. Both an Int and a UInt are 32bits. The only difference
is that one bit is used as a sign bit in the Int.
Whenever I see programs, everyone always uses the int field to
represent the userID. Should this really be a uint since the field
will never be negative, only positive?

It depends on the database field type. I guess if the database field is a
UInt, then technically, you should use a UInt as well. Depending on the
conversion routines used when loading the database field into your integer
variable, you might not get an error - simply a minus value if the value of
the field is greater than Int.MaxValue, but you could also get an overflow
exception.

My recommendation would be to defy convention and use UInt32 (or whatever
matches your database field).

Hope this helps,

Trev.
 
Hello Ender,

Maybe using the int datatype comes from the old days of VB, also VB.NET
(currently) did not support unsigned integer.
On the otherside the uint datatype is not CLS-Compliant datatype, while int
is. On the other hand you if you need only postive numbers, the uint
datatype will give you double the positive numbers when using int datatype.

Regards,




Maher
(e-mail address removed)
 
These are some of the questions that you begin to run into when you
think about it a little.

The UINT is not CLS compliant, so you run into problems there,
especially if you want a VB.NET app to utilize your object (I belive
that VB.NET does not supports UINT, may be wrong on this).

However, if you let's pretened you have a SQL Integer field that goes
beyond the max int value. Then you need to use large ints.

It seems like a simple question, but there are so many different ways
to skin this and was wondering if anyone has seen anything from
Microsoft on the best approach.

I believe all of their examples I have seen use integer, which is
probably the way to go.
 
I belive
that VB.NET does not
supports UINT, may be wrong on this.

VB.net doesn't support the UInt32 directly, but you can still use it if you
want... e.g.
------------------------------
Dim nUint as System.UInt32

nUInt = Convert.ToUInt32(1500)
-------------------------------

Mathematical opreators and other stuff won't work too well though.

HTH,

Trev.
 
Back
Top