Wolfish
Wolfish

Reputation: 970

Creating an ambiguous test

I had an idea about creating a bool isNull that can be used pretty much ambiguously wherever needed. The original idea was as follows (pseudo-code only):

bool isNull(var test)
{
    if (test == null || DBNull || string.Empty)
                 return true;
    else
                 return false;
}

But this doesn't work, as var is not recognised here. Instead, it appears to be assumed that var refers to a type... well I, of course, don't have a type for var!

What do I do to get around this? Or, perhaps the question I should be asking, Is this a good idea at all?

Upvotes: 0

Views: 35

Answers (2)

Tomtom
Tomtom

Reputation: 9394

Your code isn't working because var will be resolved at compile-time.

You could use object or dynamic as type. dynamic will be resolved at run-time

Upvotes: 0

Selman Genç
Selman Genç

Reputation: 101681

Why don't you use object ?

bool isNull(object test)
{
    if (test == null || test == DBNull.Value)
         return true;
    else
         return false;
}

For strings, I would use string.IsNullOrEmpty method.For other types, especially when you are dealing with databases this function can be useful.

Upvotes: 2

Related Questions