Problem overview
A ticket machine gives each new ticket the next number: 1, then 2, then 3. The class Ticket models it. Each ticket keeps its own name and number. The counter that hands out the numbers does not belong to any one ticket: it belongs to the class, and there is exactly one copy of it. The static method Issued() returns how many tickets were created since the last reset, and the static method ResetCounter() sets the counter back to 0.
After a reset, creating tickets for Ann, Bob and Cy gives them the numbers 1, 2 and 3, and Issued() returns 3. Two tickets for the same name still get different numbers, because the number comes from the counter, not from the name.
Approach
C# has two kinds of members. An instance member, such as name, exists once per object. A static member, marked with static, exists once per class and is shared by every object. The counter must be static: if it were an ordinary field, each new ticket would get a fresh counter starting at 0 and every ticket would be number 1.
The constructor does two things for each new ticket: it adds 1 to the shared counter, then copies the counter's new value into the ticket's own number field. The copy matters. The counter keeps changing as more tickets are made, but a ticket's number must stay what it was when the ticket was created. Reading the counter later, instead of copying it, would give every ticket the last number.
Step by step
- Declare the counter at the top of the class:
public static int Count = 0;. One copy for the whole class. - Declare the per-ticket fields: a
public string nameand apublic int number. They are not static, so every ticket has its own. - Store the name in the constructor with
this.name = name. - Increase the counter with
Count++. Inside the class you can write justCount; from outside it isTicket.Count. - Copy it into
this.number. After a reset, the first ticket finds the counter at 1 here, so numbers start at 1. IssuedreturnsCount.ResetCounterassigns 0 toCount, so the next case numbers its tickets from 1 again.
The solution in C#
class Ticket {
public static int Count = 0;
public string name;
public int number;
public Ticket(string name) {
this.name = name;
Count++;
this.number = Count;
}
public static int Issued() {
return Count;
}
public static void ResetCounter() {
Count = 0;
}
}
Using it:
Ticket.ResetCounter();
var a1 = new Ticket("Ann");
var a2 = new Ticket("Bob");
var a3 = new Ticket("Ann");
Console.WriteLine(a1.name + " #" + a1.number + ", " + a2.name + " #" + a2.number + ", " + a3.name + " #" + a3.number);
Console.WriteLine(Ticket.Issued());
Ticket.ResetCounter();
Console.WriteLine(Ticket.Issued() + " " + new Ticket("Cy").number);
This prints:
Ann #1, Bob #2, Ann #3
3
0 1
The last line shows the reset: the count is 0 until a ticket is made, and the next ticket is number 1 again.
Details worth knowing
- Calling static members. You call them on the class name:
Ticket.Issued(), with no ticket needed. You can't calla1.Issued(); C# reports an error for a static member used through an object. - Static methods can't use
this. A static method runs without any particular ticket, so it can use only static members. That is fine here, sinceIssuedandResetCounteronly touchCount. - Instance code can use static members. The constructor reads and writes
Countwithout any prefix, because every ticket can see its class's shared members. - The static counter lives as long as the program, not as long as any ticket. Creating tickets always continues from the current value, which is why a reset is needed between independent cases.
Edge cases and common mistakes
- Making the counter an instance field. Every ticket would start from 0, and every number would be 1.
- Not copying the value. A
numberthat is read fromCountwhen printing, instead of stored at creation, would show the last number for every ticket. - Incrementing in the wrong order. Copying the counter before
Count++would number the first ticket 0. - No tickets. After a reset with no tickets created,
Issued()returns 0. - Same name, different tickets. Two tickets for "Mia" are separate objects and get numbers 1 and 2.
Testing
- Reset, then Ann, Bob, Cy:
Ann #1,Bob #2,Cy #3, andIssued()is 3. - Reset, then Mia and Mia:
Mia #1,Mia #2, andIssued()is 2. - Reset and no tickets:
Issued()is 0.
Static or instance: how to decide
Ask whether a value describes one object or the whole class. A ticket's name and number describe that ticket, so they are instance fields. The count of tickets made so far describes the machine, so it is static. The same question applies to methods: Issued and ResetCounter need no particular ticket, so they are static, while a method that read a ticket's own number would be an instance method.
One consequence is that a static field can be shared by code that never creates an object. Ticket.ResetCounter() works before any ticket exists, which is exactly how the lesson's tests start each case.
