Another very nice and deep article. Let my ask you some questions for
clarification.
public List<Entry> FindEntries(DateTime date, bool byTime)
{
List<Entry> found = new List<Entry>();
foreach (Entry entry in entries)
{
if (((byTime) && (entry.Occurs == date)) // filtered by time and date
||
((!byTime) && (entry.Occurs.Date == date.Date))) // filtered by date only
found.Add(entry);
}
return found;
}
First
Why do you create a new list before entering the foreach loop? Shouldn't it just
be an Entry object?
Second
Speaking of which: Why do we need to create a new List / new object at all? My
understanding is that we are searching an existing List of Entry objects for one
that fits our search criteria, so why do we need an initialization?
Third
Why do we need to put (byTime) in brackets by it's own? I tried it like
this:
if ((byTime && (entry.Occurs == date)) //filtered by time and date
|| ((!byTime && (entry.Occurs.Date == date.Date)))) // filteresby date only
found.Add(entry);
If we created a new list in the foreach loop, in the end there'd be multiple
lists cointaining only 1 item each.
The method doesn't search for a single entry. There can be multiple entries
happening on the same day. Therefore we need to return multiple items using a
List of these items. First, we create an empty list. Then we fill it with
suitable items. And finally we return this list.
The parentheses don't have to be there, I probably added them just because
of the negation to make it more clear and then copied them to the line above as
well.