4.4 Creating, Compiling and Running a Visual C# Project with Two Classes

When you create the project for this app, you should rename Program.cs to Account-Test.cs and add the Account.cs file to the project. To set up a project with two classes:

  1. Create a Console Application as you did in Chapter 3. We named this chapter’s projects Account1, Account2, Account3 and Account4, respectively.

  2. Rename the project’s Program.cs file to AccountTest.cs. Replace the autogenerated code with class AccountTest’s code (Fig. 4.1).

  3. Right click the project name in the Solution Explorer and select Add > Class… from the pop-up menu.

  4. In the Add New Item dialog’s Name field, enter the new file’s name (Account.cs), then click Add. In the new Account.cs file, replace the auto-generated code with class Account’s code from Fig. 4.2.

You can open each class in the Visual Studio editor by double clicking the filename in the Solution Explorer window.

You must compile the classes in Figs. 4.1 and 4.2 before you can execute the app. This is the first time you’ve created an app with multiple classes. Class AccountTest has a Main method; class Account does not. The IDE automatically recognizes as the app’s entry point the class that contains Main. When you select Build > Build Solution in Visual Studio, the IDE compiles all the files in the project to create the executable app. If both classes compile correctly—that is, no compilation errors are displayed—you can then run the app by typing Ctrl + F5 to execute the AccountTest class’s Main method. If you do not build the app before running it, typing Ctrl + F5 will build the app first and run the app only if there are no compilation errors.

Common Programming Error 4.2

In a given project, declaring a Main method in more than exactly one class results in the compilation error, “Program has more than one entry point defined.”

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

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