Null, Empty

S

shapper

Hello,

Is it possible to create a method that receives an object, String,
Int, MyType?, Object, ... and:
1. If the object is null return false if not null returns true
2. If the object is a string then returns false if it is null but also
if it is empty.

Basically, I want to create a method for use in validation that checks
if any object is defined.

Thanks,
Miguel
 
A

Alberto Poblacion

shapper said:
Is it possible to create a method that receives an object, String,
Int, MyType?, Object, ... and:
1. If the object is null return false if not null returns true
2. If the object is a string then returns false if it is null but also
if it is empty.


public bool Validate(object obj)
{
if (obj is string) return !string.IsNullOrEmpty((string)obj);
return (obj!=null);
}
 
J

Jason Newell

I'm envisioning something like:

class Validator
{
static bool IsValid(object o)
{
return o == null ? false : true;
}

static bool IsValid(IntPtr p)
{
return p.Equals(IntPtr.Zero) ? false : true;
}

static bool IsValid(string s)
{
return String.IsNullOrEmpty(s) ? false : true;
}

//etc...
}

Jason Newell
www.jasonnewell.net
 
S

shapper

I'm envisioning something like:

class Validator
{
     static bool IsValid(object o)
     {
         return o == null ? false : true;
     }

     static bool IsValid(IntPtr p)
     {
         return p.Equals(IntPtr.Zero) ? false : true;
     }

     static bool IsValid(string s)
     {
         return String.IsNullOrEmpty(s) ? false : true;
     }

     //etc...

}

Jason Newellwww.jasonnewell.net

Isn't IntPtr.Zero the same as an Int which value is 0?

An int with value 0 is a valid value.
 
J

Jason Newell

From MSDN:
http://msdn.microsoft.com/en-us/library/system.intptr(VS.80).aspx

"A platform-specific type that is used to represent a pointer or a handle."

"The IntPtr type is designed to be an integer whose size is
platform-specific. That is, an instance of this type is expected to be
32-bits on 32-bit hardware and operating systems, and 64-bits on 64-bit
hardware and operating systems."

So no, you would not want to treat IntPtr like an int.

Jason Newell
Software Engineer
www.jasonnewell.net
 
G

Göran Andersson

shapper said:
Hello,

Is it possible to create a method that receives an object, String,
Int, MyType?, Object, ... and:
1. If the object is null return false if not null returns true
2. If the object is a string then returns false if it is null but also
if it is empty.

Basically, I want to create a method for use in validation that checks
if any object is defined.

Thanks,
Miguel

Checking for null is simple as long as it's a reference type. For any
other check you have to look for specific types and implement
comparisons that are relevant.

Is there any limitation on what data types there could be? A string
surely, but could it be a StringBuilder, an SqlString, a char array?
 

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

Top