Short and null

This is a discussion on Short and null within the C# Programming forums, part of the General Programming Boards category; HI!! Let say I have an object and this object has a property of the type short. How I can ...

  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 02: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 02: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,139
    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, 01:46 PM
  2. Wierd Segmentation Faults on Global Variable
    By cbranje in forum C Programming
    Replies: 6
    Last Post: 02-19-2005, 11:25 AM
  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, 09: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21