Solved

.Net Nullable - how do you use it?

Posted on 2011-02-28
4
327 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C# class library debugging - Breakpoint will not hit. 8 46
C# Formatting DateTime String Value 5 68
Create a Dropdown list with sub group 10 43
What are MicroServices? 4 68
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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…

739 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