Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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:

 

if( myObject.MyShortProperty != null )

{

 

}

 

I get an null reference exception.

 

How I can check this without a null references exception?

 

 

 

regards,

 

 

gicio

Posted
It would seem that myObject is null. Also a value type can never be null. If you want to check if a value type property has been set then just set a bool in the property set method.
"Who is John Galt?"
  • Leaders
Posted
If you are not the author of this class, then there is no way to see if the value has been set because a short can never be null. Alternatively (assuming that you are the author of this class), although I would not personally recommend this, you could declare both the property and the underlying variable it is stored in as type object which would be null until set to a short, but this can cause issues with types and introduce bugs. Best to go with the method recommended by IngisKahn.
[sIGPIC]e[/sIGPIC]

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...