Get up to 80 % extra points for free! More info:

Discussion – Lesson 13 - Diary with a database in C# .NET

Back

 

Comments
Avatar
Lennart Völler:11/29/2017 8:18

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);
 
Reply
11/29/2017 8:18
Avatar
Replies to Lennart Völler
David Capka Hartinger:11/29/2017 15:14
  1. If we created a new list in the foreach loop, in the end there'd be multiple lists cointaining only 1 item each.
  2. 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.
  3. 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.
Edited 11/29/2017 18:18
Reply
11/29/2017 15:14
You can walk through a storm and feel the wind but you know you are not the wind.
To maintain the quality of discussion, we only allow registered members to comment. Sign in. If you're new, Sign up, it's free.

2 messages from 2 displayed.