NEWBIE: Int32 won't hold nulls

Posted on 2006-04-19
Last Modified: 2010-04-16
Dear Experts,

I'm converting my world from Java to C#...

First problem:  I regularly put NULL into Java "Integer" objects, to indicate "no value".  This is different from zero.  Java does have an "int"--which is nothing but a primitive number.  It's not an "object".  However, "Integer" is a "wrapper" for "int", which basically makes "int" an object.  And the "Integer" can be NULL.

The Visual Studio 2005 Java Converter converted all my "Integer" objects to System.Int32.  My code is then trying to move NULL to that, which doesn't compile.  What is the standard way to handle this?  Using zero, or negative one, won't be good enough.  How do I indicate "this number is unassigned yet"?


Question by:BrianMc1958
    LVL 6

    Expert Comment

    Well, it has to contain a value of some sort.

    A lot of people will use something like int.MinValue and then test for int.MinValue when they pull it out of a database or something.
    LVL 12

    Expert Comment

    Yeah... an amazing simple concept that is made pretty tough in .NET.

    In 1.1 I created a Null class that has properties for all of the the types we use. and returns an obscure value that we use to distinguish a 'NULL' value.

    I believe that 2005 may have a provided class that  provides a similiar approach.

    My class looks like this

          #region public sealed class Null

          public sealed class Null
                public const int Int = -1;
                public const double Double = -1000000;
                public const string String = "StringIsNull";
                public const string Date = "01/01/1900";
                public const decimal Decimal = -0.1m;

                public Null() {}
          #endregion // public sealed class Null

    LVL 6

    Expert Comment

    actually if you're going to go for that route then you might use int.MaxValue, just in case you have (or will) use unsigned integers, at least everything can be consistent then.
    LVL 6

    Expert Comment

    ^------ was refering to my previous comment, not to topdog's, we submitted at the same time
    LVL 12

    Accepted Solution

    Studio 2005 has nullable value types.

    Can't offhand remember the syntax 'cos I'm still using 2003 until next week.... I think it might be

    int? myVariable;

    i.e. with a '?' after int

    You can then assign null to myVariable.

    Or look up nullable value types in the help :-)

    Author Comment

    Thanks, folks.  I've done a little more research on it.  It seems the '?' was added at the last minute for this VS release, specifically to accomodate what I'm trying to do: make it compatible with MS SQL, which allows numeric fields to contain nulls.  Let's hope this works!


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Article by: Ivo
    C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
    Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now