Results 1 to 4 of 4

Thread: Check if a type int is assigned a value

  1. #1

    Thread Starter
    Frenzied Member dj4uk's Avatar
    Join Date
    Aug 2002
    Location
    Birmingham, UK Lobotomies: 3
    Posts
    1,131

    Check if a type int is assigned a value

    I declare a variable:

    public int left;

    Sometimes it is assigned a value other times not. Obviously I want to catch times when it isn't assign to avoid errors but I can't find a way to do it I've tried:

    if (left != null) {

    But null can't be used with type int.

    Help!

    DJ

  2. #2
    Frenzied Member axion_sa's Avatar
    Join Date
    Jan 2002
    Location
    Joburg, RSA
    Posts
    1,724
    Perhaps give it a default value?

    Code:
    const int DefaultValueBlah = 0;
    int x = DefaultValueBlah;
    
    if (x == DefaultValueBlah) {}

  3. #3
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    No need (but it's good practice), integral datatypes are set to 0 by default , string types to "" .

  4. #4

    Thread Starter
    Frenzied Member dj4uk's Avatar
    Join Date
    Aug 2002
    Location
    Birmingham, UK Lobotomies: 3
    Posts
    1,131
    Thing is I don't really want a default value as the variable could be set as a negative or postive number or even zero. I need to be able to tell if this variable has been assigned a value or not.

    I'm thinking I might need to have a second boolean variable that holds if this value was set.

    Cheers

    DJ

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width