Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

.Net Nullable - how do you use it?

Posted on 2011-02-28
4
Medium Priority
?
336 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 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

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
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…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

704 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