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