Solved

Convert a null value to null value

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

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled

726 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