Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (edited)

This question is purely theoretical but I've become unsure whats the best way to compare datatypes (most specifically int vs string).

 

Lets say I have a some kind of collection (from a database) and everything is a string, lets say dg.Row["PersonID"] and I have a method that returns an int, called PersonID(). Please note that if I designed this database table and set the PersonID as an int in the database it's toally irrelovant. Especially when I compound the fact that you code assume this database has HAS to be an int when future changes might dictate otherwise.

 

There are two ways to go about this, I can choose to code to the lowest denominator like so:

 

if (PersonID() == Convert.ToInt32(dg.Row["PersonID"])

 

and to properly code this, I would need to try/catch my Convert.ToInt32() to be sure the data is legit.

 

Or I can cast up, where I know the conversion will (should?) always work such as

 

if (PersonID().ToString() == dg.Row["PersonID"])

 

Now while this may be a personal preference/style question, I would like to hear what you guys think is better coding... to choose the first method where data is compared on the simpliest level or should I code on the more rebust level where conversion is safer?

 

I've originally thought the best way is to convert down to the simpliest data (as the data is suppose to be all numerical, itjust so happens SQL server outputs everthing as a string even when its an int, interally to the database. But lately I'm thinking I should learn to the most rebust method, I mean isn't that what OOP is about, being robust?

Edited by travisowens
Experience is something you don't get until just after the moment you needed it
Posted

I think you were asking how you would determine and compare the type of an unknown object to make sure it was an integer????

 

I would:

 

if(PersonID().GetType()==typeof(int))
{ 
       //code
}
else
{
      //throw some sort of error.
}

//A quick console program demonstrates:

static void Main(string[] args)
{
object o = 5;
if(o.GetType()==typeof(int))
	Console.Write("Is Integer");
else
	Console.Write("Is not integer");
Console.Read();
}

Posted

bri189a,

I wasn't asking how to check if something is int or string, that's not a problem. Anyways, I feel I can trust the datatype of PersonID() because I coded it, I'm not too concerned about it's datatype (VS's Intellisense exposes it to me everytime I type it in the code editor anyways). My issue is that, normally, a database collection of data (dataset, datagrid, etc) is all strings by default, and while a certain column was designed to be int within the database, it's coming back to me as a string so I need to either convert my INT based PersonID() to a STRING and compare to the database, or convert my Database's column of a STRING into an INT and compare to PersonID().

 

Perhaps my problem is I need to enforce datatypes within my database collection, I have heard this suggestion verbally from others. If my issue does get answered verbally, I'll post a follow-up in here (the Internet has too many good questions with no resolution posted, I don't need to add to that pile).

 

Obviously casting INT to a STRING always works but is it really the most correct way (don't confuse what works with best methods) or should I downcast my string to an int, which may not work, and would need some cast checking (not shown in my code).

Experience is something you don't get until just after the moment you needed it
Posted
bri189a,

My issue is that, normally, a database collection of data (dataset, datagrid, etc) is all strings by default, and while a certain column was designed to be int within the database, it's coming back to me as a string...

 

Why would it be coming back as a string if the database column is an int? That's the part I don't understand...if I have an int column in a database I return the data as an int whether it be to a DataReader, or populating a dataset, and using a dataset I have the column set up to be an int...so it really doesn't make since that if you've designed the table, the stored procedure, or in line SQL, set up the params to be of type int then how you are they getting to be strings...that's what I would wonder about.

 

Maybe I'm missing your point, and if I am I appologize.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...