Click here to Skip to main content
15,905,877 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everybody,

I want to check if the dates are not overlap each other, otherwise give a message

I have this method:

C#
<pre lang="xml">public bool IsOverlapping(List <AgendaEventViewModel> viewModel, AgendaEventViewModel newItem)
        {
            bool isOverlapping = false;
            ReservationModel r = new ReservationModel(int.Parse(viewModel.id));
            if (r.Begin == r.Begin)
            {

            }

           // TODO: Check if Event is Overlapping with other events :) Dynamic RoomRules Check should implemented here


            if (viewModel.Any(m => DateTime.Parse(m.start) < DateTime.Parse( newItem.start) && DateTime.Parse(newItem.start) < DateTime.Parse(m.end) ?? DateTime.MaxValue)));
            {
                this.ShowMessage(Message.Type.Info, Resources.AgendaEvent.IsOverlapping);
            }

            return isOverlapping;
        }


and this is the model:


C#
public class AgendaEventViewModel
    {
        public string ReturnUrl { get; set; }

        public string id { get; set; }
        public int? funeralId { get; set; }
        public int? reservationId { get; set; }

        public int? roomId { get; set; }

        [Display(Name = "Title", ResourceType = typeof(Resources.AgendaEvent))]
        public string title { get; set; }

        [Display(Name = "Start", ResourceType = typeof(Resources.AgendaEvent))]
        public string start { get; set; }

        [Display(Name = "End", ResourceType = typeof(Resources.AgendaEvent))]
        public string end { get; set; }

        public string top { get; set; }
        public string left { get; set; }

        public List<RoomItemModel> RoomItems { get; set; }
        public List<CateringItemModel> CateringItems { get; set; }

        [Display(Name = "SeatingCapacity", ResourceType = typeof(Resources.AgendaEvent))]
        public int? SeatingCapacity { get; set; } //Max Room SeatingCapacity

        [Display(Name = "ExpectedSeatingCapacity", ResourceType = typeof(Resources.AgendaEvent))]
        public int? ExpectedSeatingCapacity { get; set; } // User Chosen
    }


but now I get the error message:

Operator '??' cannot be applied to operands of type 'bool' and 'System.DateTime'

thank you

Oke, I have it now like this:


C#
<pre lang="cs">public bool IsOverlapping(AgendaEventViewModel viewModel, AgendaEventViewModel newEvent)
        {
            bool isOverlapping = false;
           // ReservationModel r = new ReservationModel(int.Parse(viewModel.id));
             DateTime? begin = DateTime.Parse(viewModel.start);
            DateTime? end = DateTime.Parse(viewModel.end);

            DateTime? begin2 = DateTime.Parse(newEvent.start);
            DateTime? end2 = DateTime.Parse(newEvent.end);
            //DateTime dataToCheck;

           // TODO: Check if Event is Overlapping with other events :) Dynamic RoomRules Check should implemented here

            if (begin ==  begin2)
            {
                this.ShowMessage(Message.Type.Info, Resources.AgendaEvent.IsOverlapping);
                isOverlapping = true;
            }

            return isOverlapping;
        }



But If I now add a second calenderItem then everytime, even on a other day it hits every time:

C#
<pre lang="cs">this.ShowMessage(Message.Type.Info, Resources.AgendaEvent.IsOverlapping);
               isOverlapping = true;


Thank you

It seems that begin will become the same as begin2.
Posted
Updated 5-Nov-14 0:17am
v3
Comments
Laiju k 5-Nov-14 4:50am    
have you googled

https://www.google.co.in/search?q=Operator+%27%3F%3F%27+cannot+be+applied+to+operands+of+type+%27bool%27+and+%27System.DateTime%27&oq=Operator+%27%3F%3F%27+cannot+be+applied+to+operands+of+type+%27bool%27+and+%27System.DateTime%27&aqs=chrome..69i57&sourceid=chrome&es_sm=122&ie=UTF-8
[no name] 5-Nov-14 5:12am    
Yes, I did that

1 solution

DateTime is not reference and is not nullable. You have to use Nullable<datetime> or however that structure looks in C#.

?? works only if the type can be null. You should rewrite the condition so it doesn't use ?? and you'll be all set.

You're also missing the condition where new event ends after m.end but starts after m.start and where newEvent starts and ends within the models interval (if that is possible in your model)

If this helps, please take time to accept the solution. Thank you.
 
Share this answer
 
Comments
[no name] 5-Nov-14 5:57am    
Thank you for your answare, I have updated my post

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900