Django Anonymous
Django Anonymous

Reputation: 3025

Datetime nullable issue

I am sending datetime to a function like:

MyFunction((DateTime)MethodDateTime);

Whereas my MethodDateTime is of DateTime datatype and contains null value.

Because of this while executing, it gives me error nullable object must have a value.

My Functions is this:

MyFunction(DateTime abc)
{
    // Statements
}

So after surfing, what i can understand that i am forcing null to datetime. But this is my issue, sometimes i get null value as datetime, so how to deal with it?

Also when i am passing datetime directly it says

  1. The best overloaded method match for 'Big.Classes.QStr.MyFunction(System.DateTime)' has some invalid arguments
  2. cannot convert from 'System.DateTime?' to 'System.DateTime'

So because of this i am doing (DateTime)MethodDateTime

Declaration and Initialization for my datetime is DateTime? MethodDateTime = null;

EDIT:

Major declaration i have made is:

    /// <summary>
    /// Get's or set's the MethodDateTime. If no MethodDateTime is there, this
    /// attribute is null.
    /// </summary>
    DateTime? MethodDateTime
    {
        get;
        set;
    }

Upvotes: 0

Views: 1187

Answers (6)

Steve
Steve

Reputation: 216363

You could simply change the method signature to receive a nullable DateTime

MyFunction(DateTime? abc)
{
    if(abc.HasValue)
    {
        // your previous code
    }
    else
    {
       // Handle the NULL case
    }
}

However, if you really don't want to change your previous code you could simply add another method with the same name but with a nullable datetime

MyFunction(DateTime? abc)
{
     Console.WriteLine("NULLABLE version called");
}

MyFunction(DateTime abc)
{
     Console.WriteLine("NOT NULLABLE version called");
}

In this way the framework will call the correct method looking at the datatype of the variable passed

Upvotes: 2

Zotta
Zotta

Reputation: 2603

"Whereas my MethodDateTime is of DateTime datatype and contains null value."

Nope, it is DateTime? and not DateTime, wich is short for System.Nullable<DateTime> The Error occurs when you cast it from DateTime? to DateTime and it has no value.

if( MethodDateTime.HasValue)
{
    MyFunction(MethodDateTime.Value);
}
else
{
    //Handle error
}

Upvotes: 0

Dave Bish
Dave Bish

Reputation: 19656

DateTime values cannot hold null. Nullable<DateTime> (which is the same thing as DateTime?) is essentially a wrapper class, that allows you to store a ValueType or a null value.

You either need to test your DateTime? value:

if(MethodDateTime == null)
    MyFunction(DateTime.MinValue) //Pass in a sentinal value

or change your method to allow nullables:

MyFunction(DateTime? abc)
{
    ....

Upvotes: 0

user707727
user707727

Reputation: 1287

If I understand you correctly, you do not want to send in DateTime as a nullable object to MyFunction. Then you have to first check if it is null and then send in the value.

if(MethodDateTime.HasValue)
{
   MyFunction(MethodDateTime.Value);
}
else
{
   // handle this case somehow
}

Upvotes: 0

Xavier
Xavier

Reputation: 1794

You can go with nullable datetime DateTime?

Upvotes: 0

Jcl
Jcl

Reputation: 28272

You need to declare your function with parameter of type DateTime? instead of DateTime so it's a nullable.

MyFunction(DateTime? abc)
{
   // Statements
}

That's the only way to work with DateTime if you need to handle possible null values. With nullable types you have the properties HasValue (preferred to checking against null) and Value, for example:

MyFunction(DateTime? abc)
{
   if(abc.HasValue)
   {
     DateTime myDate = abc.Value;
   } else {
     // abc is null
   }
}

Upvotes: 1

Related Questions