How do you like your nulls
-
ToString is an instance method, and if the user variable is null, there will be an unhandled NullReferenceException when the ToString() is called, as you cannot call a method on a null object.
Bob Dole
The internet is a great way to get on the net.
:doh: 2.0.82.7292 SP6a
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.
-
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)) { } :thumbsup:
-
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)) { } :thumbsup:
-
Yeah it's a string and probably read UserName originally. Sorry for the confusion :-\
-
I'm not sure I get what's wrong here with the given amount of context (except that ToString probably shouldn't return null...but it certainly could if it's overloaded) :doh:
-
ToString is an instance method, and if the user variable is null, there will be an unhandled NullReferenceException when the ToString() is called, as you cannot call a method on a null object.
Bob Dole
The internet is a great way to get on the net.
:doh: 2.0.82.7292 SP6a
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
-
In a project i had to refactor: if (!string.isNullOrEmpty(User.ToString())) { } null.ToString() ?? sweet :-D
i prefer:
if(User != null && User.ToString() != "")
;P
CEO at: - Rafaga Systems - Para Facturas - Modern Components for the moment...
-
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.
-
In a project i had to refactor: if (!string.isNullOrEmpty(User.ToString())) { } null.ToString() ?? sweet :-D
Maybe they have CDO?
if (!string.IsNullOrEmpty(User.ToString().ToString().ToString().ToString())) {
// OK, now I can be sure it's a string. It's a string. A string. String.
} -
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.
:doh: 2.0.82.7292 SP6a
-
Maybe they have CDO?
if (!string.IsNullOrEmpty(User.ToString().ToString().ToString().ToString())) {
// OK, now I can be sure it's a string. It's a string. A string. String.
}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.
:doh: 2.0.82.7292 SP6a
-
ToString is an instance method, and if the user variable is null, there will be an unhandled NullReferenceException when the ToString() is called, as you cannot call a method on a null object.
Bob Dole
The internet is a great way to get on the net.
:doh: 2.0.82.7292 SP6a
-
In a project i had to refactor: if (!string.isNullOrEmpty(User.ToString())) { } null.ToString() ?? sweet :-D
I like my nulls to be null objects.
-
i prefer:
if(User != null && User.ToString() != "")
;P
CEO at: - Rafaga Systems - Para Facturas - Modern Components for the moment...
-
how about this. if( User != null && User.Length != 0) is better than checking for empty string ??? :rolleyes:
Jibesh.V.P India
:doh: Why didn't i though it?
CEO at: - Rafaga Systems - Para Facturas - Modern Components for the moment...