Solved

Convert a null value to null value

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
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 Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

776 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