4.2 Test-Driving an Account Class

A person drives a car by telling it what to do (go faster, go slower, turn left, turn right, etc.)—without having to know how the car’s internal mechanisms work. Similarly, a method (such as Main) “drives” an Account object by calling its methods—without having to know how the class’s internal mechanisms work. In this sense, the class containing method Main is referred to as a driver class. We show the Main method and its output first, so you can see an Account object in action.

To help you prepare for the larger programs you’ll encounter later in this book and in industry, we define class AccountTest and its Main method in the file AccountTest.cs (Fig. 4.1). We define the Account class in its own file as well (file Account.cs, Fig. 4.2). After we present classes AccountTest (in this section) and Account (in Section 4.3), Section 4.4 discusses how to create and build a project that contains multiple .cs source-code files. First, let’s walk through the AccountTest class.

Fig. 4.1 Creating and manipulating an Account object.

Alternate View

  1   // Fig. 4.1: AccountTest.cs
  2   // Creating and manipulating an Account object.
  3   using System;
  4
  5   class AccountTest
  6   {
  7      static void Main()
  8      {
  9         // create an Account object and assign it to myAccount
 10         Account myAccount = new Account(); 
 11
 12         // display myAccount's initial name (there isn't one yet)
 13         Console.WriteLine($"Initial name is: {myAccount.GetName()}");
 14
 15         // prompt for and read the name, then put the name in the object
 16         Console.Write("Enter the name: "); // prompt
 17         string theName = Console.ReadLine(); // read the name
 18         myAccount.SetName(theName); // put theName in the myAccount object
 19
 20         // display the name stored in the myAccount object
 21         Console.WriteLine($"myAccount's name is: {myAccount.GetName()}");
 22       }
 23     }

Initial name is:
Enter the name: Jane Green
myAccount's name is: Jane Green

4.2.1 Instantiating an Object—Keyword new

Typically, you cannot call a method of a class until you create an object of that class.1 Line 10


Account myAccount = new Account();

uses an object-creation expression


new Account()

to create an Account object, then assigns it to the variable myAccount. The variable’s type is Account—the class we’ll define in Fig. 4.2. Keyword new creates a new object of the specified class—in this case, Account. The parentheses to the right of Account are required (we’ll discuss these in Section 4.8).

4.2.2 Calling Class Account’s GetName Method

The Account class’s GetName method returns the account name stored in a particular Account object. Line 13


Console.WriteLine($"Initial name is: {myAccount.GetName()}");

displays myAccount’s initial name by calling the object’s GetName method with the expression myAccount.GetName(). To call this method for a specific object, you specify

The empty parentheses indicate that GetName does not require any additional information to perform its task. Soon, you’ll see the SetName method that does require additional information to perform its task.

When Main calls the GetName method:

  1. The app transfers execution from the expression myAccount.GetName() (line 13 in Main) to method GetName’s declaration (which we’ll study in Section 4.3). Because GetName was accessed via the object myAccount, GetName knows which object’s data to manipulate.

  2. Next, method GetName performs its task—that is, it returns the myAccount object’s name to line 13 where the method was called.

  3. Console.WriteLine displays the string returned by GetName—the name is inserted into the interpolated string in place of the call to GetName—then the program continues executing at line 16 in Main.

Because we have not yet stored a name in the myAccount object, line 13 does not display a name.

4.2.3 Inputting a Name from the User

Next, lines 16–17 prompt for and input a name. Line 17


string theName = Console.ReadLine(); // read a line of text

uses Console method ReadLine to read the name from the user and assign it to the string variable theName. The user types the name (in this case, Jane Green) and presses Enter to submit it to the app. Method ReadLine reads a whole line, including all the characters the user types until the newline that the user typed by pressing Enter—the newline is discarded. Pressing Enter also positions the output cursor to the beginning of the next line in the console window, so the program’s next output begins on the line below the user’s input.

4.2.4 Calling Class Account’s SetName Method

The Account class’s SetName method stores (sets) an account name in a particular Account object. Line 18


myAccount.SetName(theName); // put theName in the myAccount object   

calls myAccounts’s SetName method, passing theName’s value as SetName’s argument. The method stores this value in the object myAccount—we’ll see exactly where it’s stored in the next section.

When Main calls the SetName method:

  1. The app transfers program execution from line 18 in Main to method SetName’s declaration. Because method SetName was accessed via the myAccount object, SetName “knows” which object to manipulate.

  2. Next, method SetName stores the argument’s value in the myAccount object (we’ll see exactly where in Section 4.3).

  3. When SetName completes execution, program control returns to where method SetName was called (line 18 in Main), then execution continues at line 21.

Displaying the Name That Was Entered by the User

To demonstrate that myAccount now contains the name the user entered, line 21


Console.WriteLine($"myAccount's name is: {myAccount.GetName()}");

calls myAccounts’s GetName method again. As you can see in the last line of the program’s output, the name entered by the user in line 17 is displayed. When the preceding statement completes execution, the end of Main is reached, so the app terminates.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset