Results 1 to 3 of 3

Thread: [2.0] How to check for Null in a value type variable.

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2003
    Posts
    436

    Question [2.0] How to check for Null in a value type variable.

    In a Value type variables like bool, int or float how to check if there is no value in that variable yet?

    to check for a null in reference variable, we can check for null. Howz it possible for value types?

    nath

  2. #2
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: [2.0] How to check for Null in a value type variable.

    Value types are never null.

    However, in .net 2005 there are Nullable types which means that you can asign null to virtually any value type...

    PHP Code:
    intnull
    The "?" means that this integer is wrapped in an object that CAN be assigned null.

    Personally I do not recommend these nulable types unless you are doing database work, they are slow (just like variants were in VB6).
    I don't live here any more.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] How to check for Null in a value type variable.

    There's no such thing as a value type variable without a value. When you declare a variable it is created on the stack and each bit is set to zero. If the variable is a reference type then those zeroes represent null, i.e. the variable refers to no object on the heap. If the variable is a value type then those zeroes are interpreted as a value of that type. If the type is int then the value is 0, if the type is bool then the value is false, etc. If you try to test a value type for null you will get a compilation warning telling you that the test will always be false because a value type cannot be null. .NET 2.0 introduces the Nullable type to allow you to create value types variables that can be null:
    Code:
    Nullable<int> i = null; ;
    
    if (!i.HasValue)
    {
        MessageBox.Show("i has no value.");
    }
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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