travisowens Posted July 20, 2005 Posted July 20, 2005 (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 July 20, 2005 by travisowens Quote Experience is something you don't get until just after the moment you needed it
bri189a Posted July 21, 2005 Posted July 21, 2005 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(); } Quote
travisowens Posted July 21, 2005 Author Posted July 21, 2005 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). Quote Experience is something you don't get until just after the moment you needed it
samsmithnz Posted July 21, 2005 Posted July 21, 2005 If I'm comparing a number with the string, I'll always convert the string to a number, then I know I'm on equal terms. besides if one is a number, the string is probably meant to be one too. :) Quote Thanks Sam http://www.samsmith.co.nz
thenerd Posted July 21, 2005 Posted July 21, 2005 Also, if you convert both to numbers, you'll be able to do more with it, like use the < or > signs, etc. Quote
IngisKahn Posted July 21, 2005 Posted July 21, 2005 Don't forget that .NET 2.0 has int.TryParse(...) Quote "Who is John Galt?"
bri189a Posted July 21, 2005 Posted July 21, 2005 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. Quote
jmcilhinney Posted July 22, 2005 Posted July 22, 2005 If you're comparing numbers then you should compare numbers. "01" and "1" are not equal. Convert.ToInt32("01") and Convert.ToInt32("1") are equal. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.