0

I wanted to create a function, which will check the value of the parameter, if it's null it should set the value based on type of the parameter, else it should just return the value as it is.

Here what i have tried.

public static T ConvertNull<T>(T obj)
{
    if (String.IsNullOrEmpty(obj.ToString()))
    {
        HttpContext.Current.Response.Write("COMING IN");
        if (typeof(T) == typeof(Int32))
        {
            return (T)Convert.ChangeType(0, typeof(T));
        }
        else if (typeof(T) == typeof(DateTime))
        {
            return (T)Convert.ChangeType(DateTime.Now, typeof(T));
        }
        else
        {
            return (T)Convert.ChangeType(String.Empty, typeof(T));
        }
    }
    else
    {
        HttpContext.Current.Response.Write("ELSE");
        return obj;
    }
}

But the problem is it always goes in ELSE section and returns Junk value.

Can anyone tell me what's wrong with the above function.

Kevin Brechbühl
  • 4,717
  • 3
  • 24
  • 47
Abbas
  • 4,948
  • 31
  • 95
  • 161
  • 3
    `Int` and `DateTime` are non-nullable. It's not *possible* for them to be null. You conversion of an empty string to any other arbitrary type `T` is also *highly* unlikely to succeed. Most types in the world don't implement `IConvertable`. Also anything that is `null` would just throw an exception when you called `ToString` on it. – Servy Jan 21 '14 at 21:00

2 Answers2

0

String.IsNullOrEmpty(obj.ToString()) is very rarely going to be true. THe only thing I can think of that will generate an empty string vi ToString() is another empty string. In fact unless ToString() has been overridden (like it has been for native types like DateTime and int, you will get the fully qualified name of the object.

maybe you want

if (obj == default(T))

?

D Stanley
  • 149,601
  • 11
  • 178
  • 240
  • In that case, how shall i check the nulls for int and datetime – Abbas Jan 21 '14 at 21:03
  • @Abbas `int` and `DateTime` _cannot_ be null. – D Stanley Jan 21 '14 at 21:05
  • `if (obj == default(T))` would turn, for example `DateTime.MinValue` for a date into `DateTime.Now`. What if he really wanted the min date value, after all, they're not null? – Servy Jan 21 '14 at 21:05
  • @Servy - good point - the more I read the question the more confused I get. – D Stanley Jan 21 '14 at 21:06
  • ok, can anyone help with building a logic, i have some field in database like PublishDate, Views which supports datetime and int type, now if the field has null, and i try to read that value in cs code, it throws error "Object cannot be cast from DBNull to other types.", i want that if they are null and if its datetime type it should return current date, if its int it should return 0 – Abbas Jan 21 '14 at 21:11
0

If you have a nullable type that you want to replace with some value in the event that it is null, and return the existing value in the event that it is not-null, there is alerady an existing operator that does this, the ?? operator:

int? i = null;  //note of course that a non-nullable int cannot be null
int n = i ?? 0; //equals zero

string s = null;
string s2 = s ?? ""; //equals an empty string

string s3 = "hi";
string s4 = s3 ?? ""; //equals "hi"

Conveniently the ?? operator will not even compile in the event that the first operand's type is not nullable.

Servy
  • 202,030
  • 26
  • 332
  • 449