-
Short and null
HI!!
Let say I have an object and this object has a property
of the type short.
How I can check if someone set this property?
When i try to check this like that:
Code:
if( myObject.MyShortProperty != null )
{
}
I get an null reference exception.
How I can check this without a null references exception?
regards,
gicio
-
Try:
Code:
if(myObject.MyShortProperty.IsEmpty)
{
//code here
}
I should note that this will return true if you have something like:
Code:
this.button.Location = new Point(0, 0);
if(this.button.Location.IsEmpty)
{
MessageBox.Show("this.button has no location")
}
In this example the MessageBox shows because, for some reason, it reads (0,0) as a null, or empty, reference.
-
where do you get IsEmpty?
-
What do you mean?
EDIT: Blah, I'm a moron! That seems to only work for certain properties :(
EDIT2:
This might work:
Code:
if(!object.property.Equals(null))
{
//code
}
If they don't declare that property, that might work. Try it and tell me if it works.
-
Your "null reference exception" is because myObject is null. Check myObject first before you check it's properties.