Click here to Skip to main content
15,887,683 members

The Weird and The Wonderful

   

The Weird and The Wonderful forum is a place to post Coding Horrors, Worst Practices, and the occasional flash of brilliance.

We all come across code that simply boggles the mind. Lazy kludges, embarrassing mistakes, horrid workarounds and developers just not quite getting it. And then somedays we come across - or write - the truly sublime.

Post your Best, your worst, and your most interesting. But please - no programming questions . This forum is purely for amusement and discussions on code snippets. All actual programming questions will be removed.

 
GeneralStrings: the perfect way to compare numbers. Pin
OriginalGriff27-Feb-11 23:30
mveOriginalGriff27-Feb-11 23:30 
GeneralRe: Strings: the perfect way to compare numbers. Pin
fjdiewornncalwe28-Feb-11 3:25
professionalfjdiewornncalwe28-Feb-11 3:25 
GeneralRe: Strings: the perfect way to compare numbers. Pin
OriginalGriff28-Feb-11 3:30
mveOriginalGriff28-Feb-11 3:30 
GeneralRe: Strings: the perfect way to compare numbers. Pin
fjdiewornncalwe28-Feb-11 8:08
professionalfjdiewornncalwe28-Feb-11 8:08 
GeneralRe: Strings: the perfect way to compare numbers. Pin
OriginalGriff28-Feb-11 8:32
mveOriginalGriff28-Feb-11 8:32 
GeneralRe: Strings: the perfect way to compare numbers. [modified] Pin
musefan1-Mar-11 0:49
musefan1-Mar-11 0:49 
GeneralRe: Strings: the perfect way to compare numbers. Pin
GibbleCH1-Mar-11 4:13
GibbleCH1-Mar-11 4:13 
GeneralRe: Strings: the perfect way to compare numbers. [modified] Pin
musefan1-Mar-11 4:46
musefan1-Mar-11 4:46 
get rid of the var's and then we can talk Laugh | :laugh:

certainly an improvement thou you would be wasting your time checking for result.Equals("0") to end up setting it to 0 anyway, also Convert.ToDouble() will return 0 for empty string anyway (meant that for a null object). Problem is if the result is non-numeric. That's why I would lean more to a (try)parse

SqlCommand cmd = new SqlCommand("...", con);
float result = 0;
float.TryParse(cmd.ExecuteScalar().ToString(), out result);
return result;


I am not saying this is the best best way, but it covers null, empty and non-numeric values

EDIT: as I have correctly been corrected, I should test for null before using ToString() a shameful mistake...

SqlCommand cmd = new SqlCommand("...", con);
float result = 0;
float.TryParse(cmd.ExecuteScalar() AS string, out result);
return result;


GETTING THERE...

SqlCommand cmd = new SqlCommand("...", con);
float result = 0;
object cmdResult = cmd.ExecuteScalar();
if(cmdResult != null)
     float.TryParse(cmdResult.ToString(), out result);
return result;


...of course _Erik_ has the better (and smaller) amount of code
Don't vote my posts down just because you don't understand them - if you lack the superior intelligence that I possess then simply walk away
modified on Wednesday, March 2, 2011 7:10 AM

GeneralRe: Strings: the perfect way to compare numbers. Pin
GibbleCH1-Mar-11 5:56
GibbleCH1-Mar-11 5:56 
GeneralRe: Strings: the perfect way to compare numbers. Pin
musefan1-Mar-11 6:17
musefan1-Mar-11 6:17 
GeneralRe: Strings: the perfect way to compare numbers. Pin
_Erik_2-Mar-11 0:27
_Erik_2-Mar-11 0:27 
GeneralRe: Strings: the perfect way to compare numbers. Pin
musefan2-Mar-11 0:44
musefan2-Mar-11 0:44 
GeneralRe: Strings: the perfect way to compare numbers. Pin
_Erik_2-Mar-11 1:00
_Erik_2-Mar-11 1:00 
GeneralRe: Strings: the perfect way to compare numbers. [modified] Pin
_Erik_2-Mar-11 0:00
_Erik_2-Mar-11 0:00 
GeneralRe: Strings: the perfect way to compare numbers. Pin
musefan2-Mar-11 0:30
musefan2-Mar-11 0:30 
GeneralRe: Strings: the perfect way to compare numbers. Pin
_Erik_2-Mar-11 0:48
_Erik_2-Mar-11 0:48 
GeneralRe: Strings: the perfect way to compare numbers. Pin
musefan2-Mar-11 1:24
musefan2-Mar-11 1:24 
GeneralRe: Strings: the perfect way to compare numbers. Pin
_Erik_2-Mar-11 1:41
_Erik_2-Mar-11 1:41 
GeneralRe: Strings: the perfect way to compare numbers. Pin
musefan2-Mar-11 1:51
musefan2-Mar-11 1:51 
GeneralMore on string comparison Pin
oggenok6424-Feb-11 18:42
oggenok6424-Feb-11 18:42 
JokeRe: More on string comparison Pin
Bernhard Hiller24-Feb-11 21:15
Bernhard Hiller24-Feb-11 21:15 
GeneralRe: More on string comparison Pin
_Erik_24-Feb-11 23:55
_Erik_24-Feb-11 23:55 
GeneralRe: More on string comparison Pin
musefan25-Feb-11 2:39
musefan25-Feb-11 2:39 
GeneralRe: More on string comparison Pin
Samuel Cragg25-Feb-11 3:52
Samuel Cragg25-Feb-11 3:52 
GeneralRe: More on string comparison Pin
musefan25-Feb-11 4:02
musefan25-Feb-11 4:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.