NEWBIE: Int32 won't hold nulls

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"?

Thanks!
BrianMc1958

BrianMc1958Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PoeticAudioCommented:
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.
0
topdog770Commented:
Yeah... an amazing simple concept that is made pretty tough in .NET.

http://dotnetjunkies.com/WebLog/markbonafe/archive/2004/02/06/6724.aspx

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

0
PoeticAudioCommented:
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.
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

PoeticAudioCommented:
^------ was refering to my previous comment, not to topdog's, we submitted at the same time
0
andrewjbCommented:
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 :-)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
BrianMc1958Author Commented:
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!

Thanks,
BrianMc1958
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.