Domnic
Domnic

Reputation: 3867

Convert Empty to Null

Convert.ToString(icdoCalcWiz.ihstOldValues[enmCalcWiz.position_value.ToString()])!=icdoCalcWiz.position_value

Here LHS the value will come as ""(Empty)

But in RHS the value will be NULL

I have to satisfy the condition like if Empty or Null both are equal

But as per above condition both are not:

Upvotes: 0

Views: 7151

Answers (4)

syed Ahsan Jaffri
syed Ahsan Jaffri

Reputation: 1124

use this.

if (string.IsNullOrEmpty("any string"))
{
}

There is also a method String.IsNullOrWhitespace() which indicates whether a specified string is null, empty, or consists only of white-space characters.

if(String.IsNullOrWhitespace(val))
{
    return true;
}

The above is a shortcut for the following code:

if(String.IsNullOrEmpty(val) || val.Trim().Length == 0)
{
    return true;
}

Upvotes: 4

Nick Vara
Nick Vara

Reputation: 73

You can try this....

static string NullToString( object Value )
{

    // Value.ToString() allows for Value being DBNull, but will also convert int, double, etc.
    return Value == null ? "" : Value.ToString();

    // If this is not what you want then this form may suit you better, handles 'Null' and DBNull otherwise tries a straight cast
    // which will throw if Value isn't actually a string object.
    //return Value == null || Value == DBNull.Value ? "" : (string)Value;
}

YOUR CODE Will Written as....

Convert.ToString(icdoCalcWiz.ihstOldValues[enmCalcWiz.position_value.ToString()])!=
NullToString(icdoCalcWiz.position_value)

Upvotes: 1

gturri
gturri

Reputation: 14599

What about

string lhs = Convert.ToString(icdoCalcWiz.ihstOldValues[enmCalcWiz.position_value.ToString()]);
string rhs = icdoCalcWiz.position_value;

if ( (lhs == null || lhs == string.Empty) && (rhs == null || rhs == string.Empty)){
  return true;
} else {
  return lhs == rhs;
}

Upvotes: 0

Akhil
Akhil

Reputation: 2030

I think the following thread may help you regarding this

String Compare where null and empty are equal

Otherwise you can check null and assign empty string and compare

Upvotes: 0

Related Questions