Solved

Convert a null value to null value

Posted on 2009-06-29
3
621 Views
Last Modified: 2013-12-17
Hello:

I want to convert a value of a nullable type to another nullable type, in order to get a simple code avoiding to use an IF/THEN sentence.

I thought I could use the Convert class but I found out that a null is not converted to a null even when both types are nullable and can hold the null value.

In the example code below you can see how null is converted to 0 when using int?, so resulting in missing the null.

How could I achieve this?

Thanks in advance,

Juarrero

class Program

    {

        int? a;

        int? b; 
 

        static void Main(string[] args)

        {

            Program P = new Program();
 

            P.a = null;
 

            // Using Convert

            P.b = Convert.ToInt32(P.a);

            if (P.b == null) { Console.WriteLine("null"); } else { Console.WriteLine(P.b); }
 

            // Without using Convert

            P.b = P.a;

            if (P.b == null) { Console.WriteLine("null"); } else { Console.WriteLine(P.b); }
 

            Console.ReadLine();

        }

    }

Open in new window

0
Comment
Question by:juarrero
3 Comments
 
LVL 5

Assisted Solution

by:johnaryan
johnaryan earned 55 total points
ID: 24735021
I may be missing your point, but you are trying to convert from a nullable type to a nullable type with
P.b = Convert.ToInt32(P.a);
so Convert .ToInt32 has to return a valid non-nullable int

This returns the correct assignment
P.b = (int?)P.a;
0
 
LVL 3

Accepted Solution

by:
_Gerry_ earned 70 total points
ID: 24740622
In the first statement there are two steps:
   1. Convert.ToInt32() converts P.a into a real integer, implicitly converting the null into a zero.
   2. Then the assignment of this zero to a nullable int P.b just results in a zero.  

In the second statement you are just assigning nullable type to nullable type, so P.b becomes a null.

You can do standard castings between compatible types, for example:




long? l=(long?)P.b;

long? m = P.b;

int?  i = (int?)m;      // not a good idea to cast down....

int?  j = P.b as int?;   

Open in new window

0
 

Author Closing Comment

by:juarrero
ID: 31597843
johnaryan provided the first response and _Gerry_ provided a more complete one.

Thanks you both.

Juarrero
0

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

Join & Write a Comment

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

744 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