IS 350 LAB
CREATING A CONSOLE APPLICATION

Abstract

In this lab you will create and runa console application. You will complete the following tasks:

  • Create a new Visual Basic Console Application Project
  • Write code that interrogates various system characteristics and capabilities
  • Compile the application
  • Execute the application

Key Terms:

  • Class: A class is a template or blueprint for an object. A typically support methods (performs actions), and has properties (data).
  • Compiler: A compiler translates source code (Visual Basic) into an executable program.
  • Method: A method is an action perform by a class (object). Data is passed to methods via arguments. A period (.) separates and object and method name.
  • Module: A block contained by a Namespace block. A Module block contains variable declarations and procedures.
    Namespace: All .NET programs are organized hierarchically. The outermost node of this hierarchy is the namespace. The Namespace and End Namespace keywords mark a namespace block. Namespaces can be nested as in System.IO. Nested namespaces are connected together with a period (.).
  • Project: A project and solution are closely related. A Visual Studio project, when compiled, produces an executable file that can be run by the end user. A solution is used to group multiple projects so that they can be compiled and managed together.
  • Property: A property represents data stored by an object. A period (.) separates and object and property.
  • Solution: A solution is the top-level organizational unit in Visual Studio. It is used to group multiple projects to form a complete application.
  • Sub procedure: A sub procedure is a declaration marked by the Sub and End Sub keywords. Sub procedures contain variable declarations and executable statements. A Visual Studio project can contain a procedure called a startup procedure. This procedure, typically named Main, executes when the program starts. A Sub procedure block must appear inside of a Module or Class block.
  • Syntax Error: An error that violates the rules of a programming language is called a syntax error. A program containing syntax errors cannot be compiled.
  • XML: The Extensible Markup Language (XML) is used to represent data in a hierarchical way. XML is used to represent project files, solution files, and many other files that make up a Visual Studio application. XML is used by many other programming languages and development environments.

Starting Visual Studio

A Visual Studio application is built around the idea of a project and solution. Together, a Visual Studio project and solution make up an application that, when compiled and deployed, contains the necessary executable and setup files needed to run your application on another computer. In this section of the lab, you will create a new project and solution, identify the parts of the project and solution, and identify the windows that comprise the Visual Studio development environment.

HANDS-ON STEP: Starting Visual Studio and identifying the relevant windows

In the following sequence of steps, you will create a new Visual Studio project and identify the windows that comprise a project and solution.Note that this lab was prepared for Visual Studio 2015. However, the steps should be similar using Visual Studio 2012 or Visual Studio 2013. The lab was also developed using the Professional Edition of Visual Studio.

  1. Start Visual Studio 2015. The initial developer window should look similar to the following. Your window may differ slightly.

The following windows appear, by default:

  • At right side of the developer window, appears the Solution Explorer. The Solution Explorer lists all of the files that comprise your application. It can be used to select files for editing.
  • The main window is the Editor window. In the Editor window, you create and modify Visual Basic code.
  • Below the Editor Window, the Output window typically appears. The Output window display messages from the compiler and messages written to the Console window.
  • On the left side of the development environment the Toolbox window appears. The Toolbox contains items called control that allow you to create the user interface for a visual application called a Windows Forms Application. It contains no visible items because Console applications do not support .NET controls.

The windows visible within the development environment will vary based on whether the IDE is in design mode or running your application. You also have considerable flexibility on where those windows appear. Windows can be docked along an edge of the IDE. Windows can float anywhere on the desktop. The items on the View menu are used to show various windows.

Creating a New Console Application

At this point, Visual Studio is ready to create a new application – called a project. Visual Studio supports several different types of projects. These projects can be developed in different languages including Visual Basic and C# (pronounced C-Sharp).

The following list describes selected types of projects. Each of these project types are built from predefined templates and contain common template code to get you started. There are many project types beyond the ones mentioned in the following list:

  • A Windows Forms Application has a user interface made up of one or more visual forms. Visual Widgets called controls appear on a form.
  • A Windows Presentation Application is also made up of forms and controls. However, the user interface is built from a newer developer interface called the Windows Presentation Foundation (WPF).
  • A Console Application uses a Command Prompt window to read input and write output. In this lab, you will create a Console Application project.

HANDS-ON STEP: Creating an Application in Visual Studio

In this part of the lab, you will create a new project and identify the parts of a project.

  1. To create a new project click File, New, Project on the menu bar. The following dialog box appears. Note the Templates / Visual Basic folder is expanded and the Classic Desktop item is expanded.If you are using an older version of Visual Studio or a different Visual Studio edition, the available options will differ.

  2. Expand the tree in the leftmost window as shown. Select Classic Desktop. Select Console Application. Give the solution a name and folder. Click OK to create the project. Again, I suggest that you store all of your application on an External USB stick.
    As you can see, there are several templates from which you can create an application. .NET supports different programming languages and several templates for each programming language. The templates that you see will differ based on the Visual Studio version (2012, 2013, 2015, etc… ) that you are using. In addition, the templates will differ based on the Visual Studio Edition (Enterprise, Professional, Express, etc… ).. These labs assume that you are using the Visual Studio 2015 Professional Edition.
    In the controls at the bottom of the form, you tell the system how and where to create the application as follows:
  • The project and solutions files have a file name. The Name text box contains the project name and Solution name box the file name of the solution.
  • The project will be created in the folder described by Location. In the Solution drop-down box, you tell the system whether to create a new solution or add a project to an existing solution.
  • The Create directory for solution check box, if checked causes Visual Studio to create a new directory having the same name as the solution.

The following figure shows the Visual Studio Development Environment after the project has been created.

The Toolbox on the left-most side of the window is empty. Console applications do not use Toolbox controls. The Toolbox and its controls will be discussed in the next section when you create a Windows Forms application.

The window appearing at the right side of the desktop is called the Solution Explorer. It lists all of the files that make up a project in a hierarchical way.

  • At the top of the hierarchy appears the solution. The solution is named IS350ConsoleApplicationProject.
  • The solution contains one project. The project has the same name as the solution. The item named My Project is not really a filebut a folder. Visual Studio supports tools used to edit the files of this folder. You will use some of these tools in a moment. The References folder lists the references to the .NET assemblies (namespaces) required by the application.
  • The App.config file is an XML document used to configure the application. It will not be discussed further here.

The Solution Explorer is nothing more than a version of Windows Explorer used to manage the parts of a .NET application. The following figure shows this application’s files in Windows Explorer.

Note that the solution appears in the parent folder.

  • You can see the App.config file, the project file named IS350ConsoleApplication.vbproj, and the Visual Basic module file named Module1.vb.
  • The folders named bin and obj contain the executable versions of the project that are emitted from the compiler.
  • The Folder named My Project contains a number of Visual Basic or C# files used to store project settings and other information about the project.

The following figure shows a segment of the project file:

While you will never edit this file directly, it is important to understand how Visual Studio manages your application.

  • The <MyType> entry tells the compiler that the project is a (Console) project. If you created a Windows Application Project, the element’s value would be<StartupObject>IS350FirstWindowsApplication.My.MyApplication</StartupObject>.
  • The <OutputType> entry (exe) tells the compiler to produce an executable file(exe).
  • The entry <StartupObject> tells the compiler what to do when the program runs. The value IS350ConsoleApplication1 tells Visual Studio to run the module named Module1 in the namespace named IS30ConsoleApplication.
  • The value<RootNameSpace> tells the compiler the name of the root namespace. Every program has a root namespace, which should be unique.
  • The entry<TargetFrameworkVersion>tell the runtime which version of the .NET Framework to use when the program executes. v4.5.2. Every program runs under (is designed to operate with) one version of the .NET Framework class library.

The Code Editor displays the following Visual Basic code created from the template.

ModuleModule1

Sub Main()

EndSub

EndModule

  • The outermost block is the Module block.
  • The Module block contains a sub procedure (Sub) block, marked with the Sub, EndSub keywords.
  • The procedure named Main called Sub Main() is the entry point for the application.
  • The End Sub statement marks the end of the procedure block. A procedure block contains declaration and executable statements between the Sub, End Sub keywords.
  • The End Module statement marks the end of the Module block.

Importing Assemblies

.NET is built on the idea of reusable code. This reusable code is stored in assemblies. Assemblies are physical files that contain one or more logical namespaces. Your program must reference these assemblies before their namespaces can be used. In this section, you will make the necessary configuration changes to your application so that it can reference the System.Windows.Forms assembly and namespace. You will need the classes and methods it contains.

Note that in subsequent labs, you will not need to add a reference to the System.Windows.Forms assembly. It is automatically added by the template application.

HANDS-ON STEP: Referencing an Assembly

  1. In the Solution Explorer, expand the References folder as shown in the following figure:

    As you can see, there are several assemblies that have already been referenced. These predefined references were created when you created a Console application from its template.
  2. Right-click on the References folder, and click Add Reference from the context menu. The following dialog box will appear. Scroll down in the list until you see the System.Windows.Forms assembly.

  3. Highlight the line titled System.Windows.Forms. Check the check box along the left side of the Window.Make sure to check the box. Otherwise, the assembly will not be selected and you will not be able to call its methods.
  4. Click OK to add the assembly reference to your project. When you do, note that the assembly is added to the references as shown in the following figure:

Writing the Project Code

Next, you will write the application that you will later compile and execute. Before doing so, Note the following about the code that has already been written. As you will see, a Visual Basic program is made up of nested blocks.

  • One type of block is a Module block. A Module block can contain variable declarations and procedures. A Module block begins with the Module statement and ends with the End Modulestatement. Executable statements cannot appear directly inside of a Module block. They must be contained within a procedure.
  • Procedures contain executable statements. The template contains a Sub procedure named Main. As you will see, this procedure is designated as the startup procedure. A procedure block begins with the Sub statement and ends with the End Sub statement.

As you write the application’s code, pay particular attention to the Intellisense technology. Intellisense technology displays tooltips as you write code. These tooltips provide suggestions to complete an object’s properties and methods, or to complete the arguments for a method call.

Visual Basic, along with most languages has a very consistent syntax. To illustrate, look carefully at the following statement.

System.Console.WriteLine(“Hello”)

  • The name System is a namespace. System is also known as the root namespace.
  • The name Console is a class. We say that the Console class belongs to the System namespace.
  • WriteLine is a method. The WriteLine method belongs to the Console class of the System namespace.
  • The WriteLine method accepts one argument, a string that will be written to the Command Prompt window. In this case, a literal string is used. Double quotation marks surround literal strings.

All methods are called this way
namespace.class.method(arguments)

Take a look at a second example.

System.Console.WriteLine(

System.Windows.Forms.

SystemInformation.ComputerName)

This statement is not all that different from the first statement that you wrote.

  • Again, the WriteLine method of the Console class of the System namespace is called with one argument. This argument, however is not a literal string.
  • The namespace is System.Windows.Forms.
  • The class is named SystemInformation.
  • The SystemInformation class has a property named ComputerName which gets the name of the computer. The computer name is written to a Command Prompt window.

HANDS-ON STEP: Writing the application’s code

  1. Make sure that the Code Editor is active for the project. To activate the Code Editor, right-click the form named Form1 in the Solution Explorer. Click View Code from the context menu.
  2. Position the cursor on the blank line between Sub Main() and End Sub. Enter the statement shown in the following figure. Note that as you write statements, Microsoft’s intellisense technology displays ToolTips which help you to complete the statements that you are writing.
    If you make a mistake, Visual Studio will display the illegal code with a jagged maroon underscore. Syntax errors must be corrected before a program can be compiled.

  3. Now write the remaining statements that will display information about the current computer’s configuration.

As you can see from the above, all executable statements appear inside of the Sub, End Sub block. These statement display information about the computer to a Command Prompt window.

The final statement tries to read a line from the Command Prompt window causing the program to stop.

System.Console.Read()

Compiling and Executing the Project

In the next sequence of steps, you will compile the project, view the compiler output, and then run the application. When you compile a program, the source code (Visual Basic) is translated into an executable form. The output from the compiler is displayed to a window called the Output window. When you execute a program, the Output window also displays messages related to the assemblies that are loaded.

HANDS-ON STEP: Compiling a project

  1. First, click Debug, Windows, Output to display the Output window. It should appear docked along the bottom of the development window.
  2. On the menu, click Build, Build Solution. This causes Visual Studio to compile the project thereby producing an executable file. The following messages should appear in the Output window:

    As you can see in the above dialog box, the file named IS350ConsoleApplication.exe is produced in the bin\Debug directory. The following files appear in that directory.

  • The file named IS350ConsoleApplication.exe contains the executable file. If you were to double click on the file, it would execute.
  • The file named IS350ConsoleApplication.exe.config contains the application’s configuration file. This file is an XML document.
  • The file named IS30ConsoleApplication.pdb contains what is called the program debug database. The contents of this file are used to debug the application.

Next, you will run the application and display its output.