?
Solved

NEWBIE: Int32 won't hold nulls

Posted on 2006-04-19
6
Medium Priority
?
286 Views
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"?

Thanks!
BrianMc1958

0
Comment
Question by:BrianMc1958
6 Comments
 
LVL 6

Expert Comment

by:PoeticAudio
ID: 16492402
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
 
LVL 12

Expert Comment

by:topdog770
ID: 16492537
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
 
LVL 6

Expert Comment

by:PoeticAudio
ID: 16492544
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Expert Comment

by:PoeticAudio
ID: 16492552
^------ was refering to my previous comment, not to topdog's, we submitted at the same time
0
 
LVL 12

Accepted Solution

by:
andrewjb earned 500 total points
ID: 16493612
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
 

Author Comment

by:BrianMc1958
ID: 16498448
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

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
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…
Integration Management Part 2
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…

864 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