2.2. Opening a New Visual Studio Project

For this program we want to create a C# project in Visual Studio using the Console Application template. Assuming that you have brought the Visual Studio Start Page to your screen (see Figure 2.1), click the New Project button. Under Project Types, click Visual C# Projects. Under Templates, click Console Application. Change the name of the project to WordCount. You can either leave the location at the default or choose another directory in which to store your project. When done, click OK.

Figure 2.1. Visual Studio


By default the program file is named Class1.cs. It's probably better to rename it something mnemonic, like word_count.cs. You will always want to rename a project file within Visual Studio. We do this within the Solution Explorer window. The lower right-hand window in Figure 2.1, labeled 4, is the Solution Explorer window. It lists all the project files. (In Figure 2.1 our file has already been renamed.) One way to navigate between the different source files is to click on the file name within this window.

If you don't have the Solution Explorer window open, do that now by clicking on the Solution Explorer icon identified by the tricolor Mobius strip. It's on the first toolbar on the upper right portion of the screen (labeled 1 in Figure 2.1). Once the Solution Explorer window is open, right-click on Class1.cs and select Rename.

Visual Studio generates a default namespace declaration and class skeleton. I always delete those and start with an empty file. Type in the WordCount class defined in the previous section. Notice that the compiler parses your code as you enter it, alerting you to potential errors before you even build.

We also need to enter the WordCountEntry class. We'll place this part of our program in a separate file. To add a new C# file, click on the Add New Item icon. (In Figure 2.1, it is at the top left of the window, labeled 5.) Under Local Project Items, select C# Code File. Give it the name EntryPoint.cs. When done, click Open.

To build our program and fix any errors reported by the compiler, we typically use the Build command. (In Figure 2.1, it is the left-hand icon of the two that are labeled 6.) Errors and warnings are reported in the bottom Visual Studio window. If you double-click on a compiler error, the program text window displays the line on which the error occurs.

The open files are listed at the top of the program text window. Displayed on the line below the open file names are the class currently being examined and the member of that class upon which the curser is active. (In Figure 2.1, the open class is WordCount, and the active member is m_reader.)

To hop around between classes or class members independently of the file in which they are stored, we use the Class View window (labeled 3 in Figure 2.1). This window shows all the classes in our project. Each class has its members listed below it. Clicking on a class or class member brings the corresponding program text into view.

Now that the project builds without error, we'll want to execute it. Press Ctrl+F5 to execute it without starting the debugger. (The icon is the exclamation point, the right-hand icon of the two that are labeled 6 in Figure 2.1.) A console window should pop up, and the WriteLine() output of the various functions should display.

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

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