Nullable datatime reference parameter

AS 211 Reputation points
2021-01-28T11:59:44.077+00:00

All,

My method declaration is as follows:

public bool RouteCheck(ref Nullable<DateTime>  TravelTime)
{
  //Method body not relevant to the question
}

The method is called from two places. I've put some abbreviated sections of code together below:

1)

Nullable<DateTime> TravelTime=null;
RouteCheck(ref TravelTime)

2)

public DateTime PlannedArrivalTime
PlannedArrivalTime="01/01/2021 09:00:00"
RouteCheck(ref PlannedArrivalTime)

The second won't compile because "can't convert ref System.DateTime to ref System.DateTime?".

I don't understand why it won't compile. I would appreciate help on two questions:
a) A Nullable DateTime paramer should accept a null value (TravelTime in my case) or a valid date time PlannedArrivalTime in my case)?
b) If I omitted the PlannedArrivalTime="01/01/2021 09:00:00" line in the second example then PlannedArrivalTime would also be null and be an acceptable value for the method parameter?

I wondering if my syntax is wrong or I'm just not understanding the reason for the error?

I think I could solve it by making PlannedArrivalTime Nullable but that seems like a workaround rather than a proper solution.

Thanks

Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2021-01-28T14:18:43.94+00:00

    Hello,

    Try the following done in this case with .NET 5, C#9

    Helper class

    public static class DateHelpers
    {
        public static bool RouteCheck(ref DateTime? travelTime) => travelTime.HasValue;
        public static DateTime? TryParse(string text) => DateTime.TryParse(text, out var date) ? date : (DateTime?)null;
    }
    

    Program.cs

    class Program
    {
        static void Main(string[] args)
        {
    
            Nullable<DateTime> TravelTime = null;
            Console.WriteLine(DateHelpers.RouteCheck(ref TravelTime));
    
            TravelTime = DateTime.Now;
            Console.WriteLine(DateHelpers.RouteCheck(ref TravelTime));
    
            string someDate = "01/01/2021 09:00:00";
    
            var test = DateHelpers.TryParse(someDate);
            if (test.HasValue)
            {
                Console.WriteLine(DateHelpers.RouteCheck(ref TravelTime));
            }
            else
            {
                Console.WriteLine("Not a date time");
            }
    
    
            someDate = "";
    
            test = DateHelpers.TryParse(someDate);
            if (test.HasValue)
            {
                Console.WriteLine(DateHelpers.RouteCheck(ref TravelTime));
            }
            else
            {
                Console.WriteLine("Not a date time");
            }
            Console.ReadLine();
        }
    }
    

    Edit

    In regards to checking the type if there are other types e.g. int use int.TryParse or perhaps a generic check where T is the type.

    using System;
    
    namespace YourNamespace
    {
        public static class DateHelpers
        {
            public static bool RouteCheck(ref DateTime? travelTime) => travelTime.HasValue;
            public static DateTime? TryParse(string text) => DateTime.TryParse(text, out var value) ? value : (DateTime?)null;
            public static bool Is<T>(this string value)
            {
                if (string.IsNullOrEmpty(value)) return false;
                var conv = System.ComponentModel.TypeDescriptor.GetConverter(typeof(T));
    
                if (!conv.CanConvertFrom(typeof(string))) return false;
    
                try
                {
                    conv.ConvertFrom(value);
                    return true;
                }
                catch
                {
                    // ignored
                }
                return false;
            }
        }
    }
    
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Viorel 122.6K Reputation points
    2021-01-28T15:59:27.663+00:00

    DateTime is a struct, not a class, therefore it is never null. You can use ‘Nullable<DateTime>’ or ‘DateTime?’ to have a nullable value.

    0 comments No comments

  2. Petrus 【KIM】 546 Reputation points
    2021-01-29T04:20:05.083+00:00

    Compare this....
    int is not equal Nullable<int>

        private bool f1(int n)
        {
            return true;
        }
    
        private bool f1(int? n)
        {
            return false;
        }
    
    
                   int? n = null;
                    n = 0;
                    int m = 1;
                    f1(n);
                    f1(m);
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.