Compare and Null

S

shapper

Hello,

I created a method to check if two objects (string, int, ...) are
equal.

The objects should only be compared if they are not null or in case of
a string also empty.

I am using the following:

// Equal
public static bool Equal(object value, object compare) {

CaseInsensitiveComparer comparer =
CaseInsensitiveComparer.DefaultInvariant;

if (value == null ? true : (String.IsNullOrEmpty(value.ToString
()) ? true : false)) return true;

if (compare == null ? true : (String.IsNullOrEmpty
(compare.ToString()) ? true : false)) return true;

return comparer.Compare(value, compare) == 0;

} // Equal

This is working but am I checking all the cases where the Compare
would throw an error?

Thanks,
Miguel
 
G

Göran Andersson

shapper said:
Hello,

I created a method to check if two objects (string, int, ...) are
equal.

The objects should only be compared if they are not null or in case of
a string also empty.

I am using the following:

// Equal
public static bool Equal(object value, object compare) {

CaseInsensitiveComparer comparer =
CaseInsensitiveComparer.DefaultInvariant;

if (value == null ? true : (String.IsNullOrEmpty(value.ToString
()) ? true : false)) return true;

if (compare == null ? true : (String.IsNullOrEmpty
(compare.ToString()) ? true : false)) return true;

That's one of the most convoluted ways of writing a conditions I have
seen...

if (value == null || string.IsNullOrEmpty(value.ToString())) return true;
if (compare == null || string.IsNullOrEmpty(compare.ToString())) return
true;

But are you sure that those should return true? That means that
Equal("Hello", "") and Equal(DbNull.Value, "anything") return true...
return comparer.Compare(value, compare) == 0;

That only works if one of the objects implements the IComparable interface.
 
M

Morten Wennevik [C# MVP]

shapper said:
Hello,

I created a method to check if two objects (string, int, ...) are
equal.

The objects should only be compared if they are not null or in case of
a string also empty.

I am using the following:

// Equal
public static bool Equal(object value, object compare) {

CaseInsensitiveComparer comparer =
CaseInsensitiveComparer.DefaultInvariant;

if (value == null ? true : (String.IsNullOrEmpty(value.ToString
()) ? true : false)) return true;

if (compare == null ? true : (String.IsNullOrEmpty
(compare.ToString()) ? true : false)) return true;

return comparer.Compare(value, compare) == 0;

} // Equal

This is working but am I checking all the cases where the Compare
would throw an error?

Thanks,
Miguel

Hi Miguel,

If null is not allowed this comparing algorithm should be sufficient

public static bool Equal(object value, object target)
{
if (value == null || target == null)
return false;

CaseInsensitiveComparer comparer =
CaseInsensitiveComparer.DefaultInvariant;
return comparer.Compare(value, target) == 0;
}

It would consider two empty strings to be equal, but if that is not
desirable add

if(value.ToString() == String.Empty || target.ToString() == String.Empty)
return false;

If you also won't compare two different types add a

if(value.GetType() != target.GetType())
return false;
 
M

Morten Wennevik [C# MVP]

shapper said:
Hello,

I created a method to check if two objects (string, int, ...) are
equal.

The objects should only be compared if they are not null or in case of
a string also empty.

I am using the following:

// Equal
public static bool Equal(object value, object compare) {

CaseInsensitiveComparer comparer =
CaseInsensitiveComparer.DefaultInvariant;

if (value == null ? true : (String.IsNullOrEmpty(value.ToString
()) ? true : false)) return true;

if (compare == null ? true : (String.IsNullOrEmpty
(compare.ToString()) ? true : false)) return true;

return comparer.Compare(value, compare) == 0;

} // Equal

This is working but am I checking all the cases where the Compare
would throw an error?

Thanks,
Miguel

Actually, in addition to my other posts, comparers often require the target
type to be the same type as the value so. To sum it up:

public static bool Equal(object value, object target)
{
if (value == null || target == null)
return false;

if (value.GetType() != target.GetType())
return false;

if (!(value is IComparable))
return false;

CaseInsensitiveComparer comparer =
CaseInsensitiveComparer.DefaultInvariant;
return comparer.Compare(value, target) == 0;
}
 
G

Göran Andersson

Morten said:
Hi Miguel,

If null is not allowed this comparing algorithm should be sufficient

public static bool Equal(object value, object target)
{
if (value == null || target == null)
return false;

CaseInsensitiveComparer comparer =
CaseInsensitiveComparer.DefaultInvariant;
return comparer.Compare(value, target) == 0;
}

That still throws an exception if none of the obejcts implement the
IComparable interface.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top