Struct list

AS 211 Reputation points
2021-01-08T20:43:34.367+00:00

All,

Firstly people may realise that I posted a similar question here https://learn.microsoft.com/en-us/answers/questions/223984/struct-inheritence.html

As far as I can work out this is a separate issue. If it's the same issue then I've made a mistake. I'm not intending to ignore the answer to the linked question or repost the same question.

My code is as follows:

public class Location  
    {  
      protected struct Booking  
      {  
        public DateTime StartTime;  
        public DateTime EndTime;  
        public Flight BookedFlight;  
      }  

}  
  class BoardingGate : Location  
  {  

    protected List<Booking> BookingList;  


    public override Boolean SpaceAvailable(DateTime StartTime, DateTime EndTime, Flight RequestingFlight)  
    {  

      List<Booking> ExistingBooking =   
      (from Booking in BookingList  
       where (Booking.StartTime > StartTime **<<Error here**  
      && Booking.StartTime < EndTime)  
      || (Booking.EndTime > StartTime  
      && Booking.EndTime < EndTime)  
      select Booking).ToList();  

      if (ExistingBooking.Count()==0)  
      {  
        Booking NewBooking;    
        NewBooking.StartTime = StartTime;  
        NewBooking.EndTime = EndTime;  
        NewBooking.BookedFlight= RequestingFlight;  
        BookingList.Add (NewBooking);  

        return true;  
      }  
      else  
      {  
        return false;  
      }  

    }  

  }  

My intention is to have a list (named Bookinglist) of instances of the Booking structure.

The first time the SpaceAvailable method runs the list is empty (which is intended).

I thought that the first time the method runs the linq command would see that there are no items in the list and therefore not try to access any of the structure properties. Instead it's giving the error. I'm not sure if?:

a) I've misunderstood how Linq works. I tend to think of it as a For Each loop or a SQL statement neither of which would complain if the list/table are empty.

b) There is a mistake in the linq

c) I've made a mistake with the structure configuration

I realise that I could probably remove the error by putting a constructor on the struct definition and setting default values. At the moment I'm focusing more on understanding the cause of the error than fixing it.

I would also appreciate advise on the following:

1) As each structure on the list will only be added or removed I think I'm within the guidelines of keeping a structure immutable?
2) If the derived class did change a value in one of the structures in the list would it be working on a copy or a reference? Sorry I did search but I couldn't work out find whether derived classes access the original structure or a copy.

Thanks

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,011 questions
{count} votes

Accepted answer
  1. Bonnie DeWitt 811 Reputation points
    2021-01-08T23:30:59.37+00:00

    Hi @AS ,

    The LINQ query won't work if BookingList is null. But, it's easy enough to create an empty BookingList to begin with, and that is the proper way to handle this situation.

    Either initialize it to begin with:

    protected List<Booking> BookingList = new List<Booking>();  
    

    Or you could instantiate it in your method:

    public override Boolean SpaceAvailable(DateTime StartTime, DateTime EndTime, Flight RequestingFlight)  
    {  
        if (BookingList == null)  
            BookingList = new List<Booking>();  
      
        // The rest of your code is the same  
    

    Hope that helps!


    ~~Bonnie DeWitt [MVP since 2003]

    http://geek-goddess-bonnie.blogspot.com


0 additional answers

Sort by: Most helpful

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.