|
Right, but there's no reason to expect it might be null at this piece of code without more context. The previous line could be something that gets that user from a function that always returns an non-null object...I just don't think there's enough information to comment on the code as-is.
|
|
|
|
|
I understand your logic, but User can be null.
But either way turned ToString in this situation is also completely redundant because IsNullOrEmpty already does 2 specific checks. Is the item null, or is the item an empty string.
So the correct code, without triggering a NullReferenceException should have been
if (!string.IsNullOrEmpty(User)) {
}
|
|
|
|
|
Oh is User a string? If so I get it now...
|
|
|
|
|
Yeah it's a string and probably read UserName originally. Sorry for the confusion
|
|
|
|
|
Calling ToString() on a string is totally unexpected. Perfectly valid code, but I'd wonder about the programmer who wrote it.
How that line ever got written in the first place made a lot more sense when I was thinking User wasn't a string -- you can't pass a non-string to IsNullOrEmpty(). Still broke, but I can see someone making such a mistake if in a hurry. Any chance it wasn't a string at one time?
We can program with only 1's, but if all you've got are zeros, you've got nothing.
|
|
|
|
|
You are correct. That is the existing implementation (or null handling). In my opinion, it would be nice to allow the conversion (.ToString()) from null to an empty string. When something is nothing (null), it is logical to think it is an empty in terms of a string. However, this ertainly not applicable to other data types.
TOMZ_KV
|
|
|
|
|
True. That would be useful.
Bob Dole The internet is a great way to get on the net.
2.0.82.7292 SP6a
|
|
|
|
|
Zac Greve wrote: ToString is an instance method
Replace it with an extension-method
|
|
|
|
|
Returning null from a ToString would be worthy of a WTF of its own.
|
|
|
|
|
i prefer:
if(User != null && User.ToString() != "")
|
|
|
|
|
how about this.
if( User != null && User.Length != 0) is better than checking for empty string ???
Jibesh.V.P
India
|
|
|
|
|
Why didn't i though it?
|
|
|
|
|
Maybe they have CDO?
if (!string.IsNullOrEmpty(User.ToString().ToString().ToString().ToString())) {
}
|
|
|
|
|
CDO. It's like OCD, just in alphabetical order like it should be!
Bob Dole The internet is a great way to get on the net.
2.0.82.7292 SP6a
|
|
|
|
|
|
Select Case dataSetNPReport1.Spider.Rows.Count
Case 0
xrRichText1.Text = String.Format("Fråga {0}Landsting{0}Riket{0}Frågetext{1}A3{0}{0}{0}{0} - - {0} - {0}{0} - ", Chr(9), Chr(13))
Case 1
xrRichText1.Text = String.Format("Fråga {0}Landsting{0}Riket{0}Frågetext{1}{2}{0}{4}{0}{0}{5}{0}{3}", Chr(9), Chr(13), dataSetNPReport1.Spider.Rows(0).Item(0), Replace(dataSetNPReport1.Spider.Rows(0).Item(2).ToString, "*", ""), dataSetNPReport1.Spider.Rows(0).Item(5), dataSetNPReport1.Spider.Rows(0).Item(6))
Case 2
xrRichText1.Text = String.Format("Fråga {0}Landsting{0}Riket{0}Frågetext{1}{2}{0}{4}{0}{0}{5}{0}{3}{1}{6}{0}{8}{0}{0}{9}{0}{7}", Chr(9), Chr(13), dataSetNPReport1.Spider.Rows(0).Item(0), Replace(dataSetNPReport1.Spider.Rows(0).Item(2).ToString, "*", ""), dataSetNPReport1.Spider.Rows(0).Item(5), dataSetNPReport1.Spider.Rows(0).Item(6), dataSetNPReport1.Spider.Rows(1).Item(0), Replace(dataSetNPReport1.Spider.Rows(1).Item(2).ToString, "*", ""), dataSetNPReport1.Spider.Rows(1).Item(5), dataSetNPReport1.Spider.Rows(1).Item(6))
Case 3
xrRichText1.Text = String.Format("Fråga {0}Landsting{0}Riket{0}Frågetext{1}{2}{0}{4}{0}{0}{5}{0}{3}{1}{6}{0}{8}{0}{0}{9}{0}{7}{1}{10}{0}{12}{0}{0}{13}{0}{11}", Chr(9), Chr(13), dataSetNPReport1.Spider.Rows(0).Item(0), Replace(dataSetNPReport1.Spider.Rows(0).Item(2).ToString, "*", ""), dataSetNPReport1.Spider.Rows(0).Item(5), dataSetNPReport1.Spider.Rows(0).Item(6), dataSetNPReport1.Spider.Rows(1).Item(0), Replace(dataSetNPReport1.Spider.Rows(1).Item(2).ToString, "*", ""), dataSetNPReport1.Spider.Rows(1).Item(5), dataSetNPReport1.Spider.Rows(1).Item(6), dataSetNPReport1.Spider.Rows(2).Item(0), Replace(dataSetNPReport1.Spider.Rows(2).Item(2).ToString, "*", ""), dataSetNPReport1.Spider.Rows(2).Item(5), dataSetNPReport1.Spider.Rows(2).Item(6))
... You get the pattern...
People say nothing is impossible, but I do nothing every day.
|
|
|
|
|
I think I threw up in my mouth, a little.
If you get an email telling you that you can catch Swine Flu from tinned pork then just delete it. It's Spam.
|
|
|
|
|
A: It goes BOOM!
<context>SQL Server 2008 R2
I was just altering some of my code to choose betewen two values via COALESCE. Then I decided I might want to back out the change, but I wanted to leave the existing code in case I wanted to reapply the change later. So I had something like COALESCE ( /* X , */ Y ) Z -- of course, it makes no sense to COALESCE on one value, but I was only going to leave it there momentarily and I don't mind causing errors in order to advance my knowledge of the syntax.
The result is Incorrect syntax near ')'. -- I thought that was an odd message.
I checked the documentation: COALESCE ( expression [ ,...n ] ) -- so the syntax should be legal!
But it also goes on to say that COALESCE is equivalent to a CASE and my attempts at
CASE (WHEN Y IS NOT NULL) THEN Y END Z
CASE ELSE Y END Z
have been unsatisfactory.
|
|
|
|
|
Here's a single line from a horribly formatted SQL view (copied verbatim, minus 58 leading spaces):
WHERE (MarketingGroupAutoID IN (42))))))) OR
Now that you know the answer (42), we just have to figure out the question. Any theories?
|
|
|
|
|
The answer to life, the universe, and everything else?
Bob Dole The internet is a great way to get on the net.
2.0.82.7292 SP6a
|
|
|
|
|
|
How many roads must a man walk down?
Don't comment your code - it was hard to write, it should be hard to read!
|
|
|
|
|
(SQL Server 2008 R2.)
(CASE WHEN cTT.Total IS NOT NULL THEN cTT.Total ELSE 0 END) as 'Total_Total'
A dozen of these in a view...
|
|
|
|
|
What exactly are they selling? Give me the URL. Please. I would like to buy that Mars rover for null.
"Bastards encourage idiots to use Oracle Forms, Web Forms, Access and a number of other dinky web publishing tolls.", Mycroft Holmes[ ^]
|
|
|
|
|
I'm sure you can just go pick one up; there's no security around.
|
|
|
|