using string instead of int

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?
 
P

Patrice

And the value in the table is ? IMO it's best to always work with the
intended type (that is int if your database column is an int)....
 
M

Mark Rae [MVP]

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.

I guess there's nothing intrinsically wrong with it, but I can't imagine why
you would want to do this...
 
S

Seth Williams

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
 
M

mavrick_101

Just to avoid unneccary Convet.ToInt32, while retrieving from an Int type
column in table.
 
S

Stan

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.
 
L

Lloyd Sheen

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
 
M

mavrick_101

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

Lloyd Sheen

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
 
P

Patrice

Humm how do you retrieve the data ? You shouldn't have to convert. You may
want to post a new thread about your original issue...

Actually I never even thought about not having the correct mapping (and I
don't even remember to have heard about doing such a thing)...

It has a potential to cause loads of problem (comparison, sort, type
checking etc...) or at best you'll have to change your code as soon as you
wan't to do actually something with this data + the conversion even if you
have to do one is not that a huge issue...

I would really use the correct mapping between the client side variables and
the server side data. As said earlier I suspect you have an earlier issue
you may want to post about...
 
H

Hans Kesting

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 :
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top