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

x
?
Solved

.Net Nullable - how do you use it?

Posted on 2011-02-28
4
Medium Priority
?
339 Views
Last Modified: 2012-05-11
If you declare a variable (for example) DateTime? myDate, I can't seem to do anything with it unless I explicity cast it back to a DateTime:
DateTime? myDate = objectWhichMayMakeNullOrDateTime.GetVal();
if(myDate!=null)
{
     string myString=    myDate.ToString("hh:mm"); //fails
}
What good is the nullable operator here? What is a better way of doing this?
0
Comment
Question by:Silas2
  • 2
4 Comments
 
LVL 13

Expert Comment

by:Naman Goel
ID: 34996511
use like following :


DateTime? myDate = objectWhichMayMakeNullOrDateTime.GetVal();
if(myDate!=null)
{
     string myString=    myDate.Value.ToString("hh:mm"); //fails
}

or use HasValue property for this
            if (myDate.HasValue)
            {
                string myString = myDate.Value.ToString("hh:mm"); //fails
            }


refer to following MSDN link for detail of its usage.
0
 
LVL 13

Accepted Solution

by:
Naman Goel earned 400 total points
ID: 34996515
0
 
LVL 52

Assisted Solution

by:Carl Tawn
Carl Tawn earned 400 total points
ID: 34996629
Nullable objects are generally used for scenarios where an object may not have a value. Specifically for a DateTime? it marks the difference between the variable having a default value and not having a value at all.

The nullable types are generic types and are therefore objects in themselves. You use the "Value" property to return the actual value of the variable and the "HasValue" property to determine if the value is NULL or not.
0
 
LVL 11

Assisted Solution

by:Sudhakar Pulivarthi
Sudhakar Pulivarthi earned 200 total points
ID: 34996644
Hi,

objectWhichMayMakeNullOrDateTime.GetVal() method should return DateTime? and check what it is returning.
usually a value type should have a value. it cannot have null except string (which is not a value type in real terms) When in our app we come across a requirement to know whether or not user has provided a value (During xml interfacing/datatable). We use Nullable which accepts one more additional value null

Thuse int? will accept 0, integer and also null. When user has not provided a value then we can set it as null. In its absence we have option to set some value like 0,-1 which is also a valid integer.

Check whether its your requirement is like the above one then u go a had in using it, else unnessarily it hurts performance in boxing/unboxing the values.

Refer: http://msdn.microsoft.com/en-us/library/b3h38hb0.aspx
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Integration Management Part 2
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

963 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