using string instead of int

  • Thread starter Thread starter mavrick_101
  • Start date Start date
M

mavrick_101

Often in code, when retrieving a value from table where I don't have to do
any math calculations, I store the value in a string variable instead of
converting to an int and then storing in an int variable.

Do you guys see anything wrong with this practice?
 
Other than math calculations, sorting will definitely be a problem, since
text is sorted much differently than numbers
For instance 111 will come before 20, when sorting by text


David Wier
http://aspnet101.com
http://iWritePro.com - One click PDF, convert .doc/.rtf/.txt to HTML with no
bloated markup
 
Just to avoid unneccary Convet.ToInt32, while retrieving from an Int type
column in table.
 
Often in code, when retrieving a value from table where I don't have to do
It's an inefficient way to store an integer. A string requires more
memory and requires more complex (compiled) code to handle it.

So it makes no sense to do it just for the sake of it.
 
mavrick_101 said:
Just to avoid unneccary Convet.ToInt32, while retrieving from an Int type
column in table.

General rule of thumb. Do your database design. If a field is numeric then
use a number type. Don't mix presentation and data. Designing a database
for presentation sake will eventually come back to bite you and it is much
simpler to fix the presentation layer than your data.

LS
 
No, the data in the tables is still <B>int</B> or whatever. I'm talking about
when retrieving in .aspx code.
 
mavrick_101 said:
No, the data in the tables is still <B>int</B> or whatever. I'm talking
about
when retrieving in .aspx code.

Sorry I am talking about database tables.
LS
 
When your database column has an "int" type, you can easily do
something like:

DataRow dr = MyDataSet.Rows[0];
int i = (int)dr["MyIntColumn"];

but this will fail if there are "null"s in that column (the field will
then contain a DbNull.Value). In that case use this:

int ?i2 = dr["MyIntColumn"] as int?;

Hans Kesting


mavrick_101 explained :
 
Back
Top