Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Convert a null value to null value

Posted on 2009-06-29
3
627 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
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…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

840 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