Solved

.Net Nullable - how do you use it?

Posted on 2011-02-28
4
324 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

Industry Leaders: 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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

685 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