Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Depends. Say your building will only ever have one elevator. You'd want to do something like this:</p> <pre><code>public class Building { public Elevator Elevator { get; set; } } </code></pre> <p>Then when you create the building like you did in your code above, you can do something like this:</p> <p><code>office.Elevator = new Elevator();</code></p> <p>You're new to C#, so you may not have really been exposed to <code>Properties</code> yet (<a href="http://msdn.microsoft.com/en-us/library/x9fsa0sw%28v=vs.100%29.aspx" rel="nofollow">more reading</a>). Cliffs on Properties: they're creating a way for you to get and set data <em>about</em> your object. In this example, we're getting/setting the <code>Elevator</code>.</p> <p>Now, if your building is going to have an unknown amount of elevators, you can't just write properties for Elevator1 to ElevatorInfinity. That's when you'll want to use a collection of some sort. As others have posted in here, you can do this like so:</p> <pre><code>public class Building { public IList&lt;Elevator&gt; Elevators { get; set; } } </code></pre> <p>And to add an elevator to your building:</p> <pre><code>// Make sure you instantiate the list! For practice, you should run this code without instantiating the list, so you can see what happens. office.Elevators = new List&lt;Elevator&gt;(); office.Elevators.Add(new Elevator()); </code></pre> <p><a href="http://msdn.microsoft.com/en-us/library/system.collections.ilist%28v=vs.100%29.aspx" rel="nofollow">More reading on IList</a></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