Thread: Short and null

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    101

    Question 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

  2. #2
    C(++)(#)
    Join Date
    Jul 2004
    Posts
    309
    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.
    Last edited by 7smurfs; 03-13-2005 at 03:13 PM.
    To code is divine

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    101

    Question

    where do you get IsEmpty?

  4. #4
    C(++)(#)
    Join Date
    Jul 2004
    Posts
    309
    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.
    Last edited by 7smurfs; 03-13-2005 at 03:33 PM.
    To code is divine

  5. #5
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    Your "null reference exception" is because myObject is null. Check myObject first before you check it's properties.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  2. Wierd Segmentation Faults on Global Variable
    By cbranje in forum C Programming
    Replies: 6
    Last Post: 02-19-2005, 12:25 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM