Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Convert a null value to null value

Posted on 2009-06-29
3
Medium Priority
?
634 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 220 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 280 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Integration Management Part 2
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

773 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