Solved

.Net Nullable - how do you use it?

Posted on 2011-02-28
4
310 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 100 total points
ID: 34996515
0
 
LVL 52

Assisted Solution

by:Carl Tawn
Carl Tawn earned 100 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 50 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

744 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now