Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I think you have a flaw in the design.</p> <p>The DVD and BluRay classes really should be either:</p> <ol> <li>A single class with a <code>Type</code> property, probably an enum that would contain DVD and BluRay. This way, in several years once you get a new media, you can just add a value to the Enum and your application will be up to date.</li> <li>Two different classes that implement a custom interface that you could call, say, <code>IMedia</code>.</li> </ol> <p>Also, I highly suggest you take advantage of the <code>List</code> object in C# instead of arrays. It's very fast and you can add/remove items easily without having to resize your array.</p> <p>Here's a lesson on Linq: <a href="http://www.functionx.com/csharp/linq/Lesson09.htm" rel="nofollow">http://www.functionx.com/csharp/linq/Lesson09.htm</a></p> <p>Here's how I would create the Media class:</p> <pre><code>public class Media { public enum MediaType { DVD, Bluray } public MediaType TypeOfMedia { get; set; } public string Director { get; set; } public string Title { get; set; } public Media(string Title, string Director, MediaType TypeOfMedia) { this.TypeOfMedia = TypeOfMedia; this.Director = Director; this.Title = Title; } } </code></pre> <p>And here's an example how to use it:</p> <pre><code> List&lt;Media&gt; data = new List&lt;Media&gt;(); results.Add(new Media("Movie 1", "John D", Media.MediaType.DVD)); results.Add(new Media("Movie 2", "John D", Media.MediaType.DVD)); results.Add(new Media("Movie 3", "SomeOtherDirector", Media.MediaType.Bluray)); results.Add(new Media("Movie 4", "John D", Media.MediaType.Bluray)); IEnumerable&lt;Media&gt; listDirectors = from media in data where media.Director == "John D" select media; foreach (Media media in listDirectors) Console.WriteLine(media.Title); </code></pre> <p>Another example, your "List DVD" and "List Bluray" functions could really be a single function:</p> <pre><code> private void ListMediaByType(List&lt;Media&gt; data, Media.MediaType type) { foreach (Media media in data.Where(media =&gt; media.TypeOfMedia == type)) Console.WriteLine(media.Title); } </code></pre> <p>As you see, these techniques simplifies querying by a LOT. :)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload