Visual Studio 2010 Basics

M33

[ Verified Seller ]
Staff member
Trusted Seller
Joined
11 yrs. 8 mth. 22 days
Messages
5,010
Reaction score
11,818
Wallet
13,191$
Visual Studio 2010 Basics

Ajay YadavMarch 25, 2013



Introduction

This article will demonstrate the development life cycle for various .NET framework applications,for instance, executables, console applications,Windows Forms, etc., and how to build them in C# using .NET framework compilation features. This chapter will also investigate the entry point of C# programs in depth and take a closer look on other compilation options used during command line execution. By the end of this chapter, you should obtainsufficient understanding of C# to write simple programs using Notepad or VS2010 IDE without using any advance Object-oriented features.

Prerequisites

In order to execute console basedWindows Forms or other .NET applications, you need to have the following: Either Windows 7, 8 or Win 2008 Server OS (latest service pack)
.NET Framework 3.5 SDK or latest
Visual Studio 2010 IDE
SQL Server 2008 (optional)
Notepad++ (optional)
At least 2GB RAM

Building C# Applications

As a programmer, you can choose various tools to develop C# base applications such as Visual Studio 2010 IDE, Notepad++, or even Text Pad.Visual Studio 2010 is a more convenient tool in writing C# applicationsthough, because it provides multiple features such as Intellisense support, better debugging tools and many other important utilities.

There is common misconception among the rookie programmers that it is not possible to build a program in C# without purchasing a licensed copy of Visual Studio, but the truth is that you can build any .NET program by downloading the free .NET framework software development kit. You can develop console based applications, Windows Forms and class libraries (DLL) using the .NET SDK.

Building C# Application using Csc.exe

Sometimes you don’t have a copy of VS 2010 or you might be in an environment where you are prohibited from using code generation tools.In this case, you can create a simple C# program in the traditional method by typing the code into a text editor such as Notepad. The following code simply displays the message “Hello World”on the screen:

Code:
Please, Log in or Register to view codes content!

Once you have finished coding, save this file with a “.cs” extension in any directory. Then, compile this program by running the C# command line compiler (csc.exe) against the source file:.

csc.exe test.cs(csc.exe /target:exetest.cs)

After successfully compiling that file, the test.exe file is created in the project folder which produces the output. The file can be customized by applying other compilation options such as the following:

dadada.jpg


You can also reference external assembly into the program. Suppose you want to display a message box via console based application – you have to import the associate namespace or class name into the program to utilize their functionality, as follows:

Code:
Please, Log in or Register to view codes content!

Building Console Application using Visual Studio

We can develop console applications using Visual Studio 2010 IDE, which provides a better interface for coding along with other features such as compile time debugging, Intellisense, etc. Generally, it is easier to develop applications under Visual Studio IDE rather than onNotepad. To create an application, follow these steps:

1.Open the Visual Studio 2010 program.
2.Then go to File menu,click on Projectand select Console Application. The following dialog box opens:

032513_1141_VisualStudi1.png


In the New Project dialog box, you can set options such as the language in which you want to write code, the framework, the project file location and the kind of application you’re making.
3.Select the location in which the application file will be stored and give it a project name, testApp in this example.
4.After that, the VS2010 IDE creates a program.cs file and imports some default namespace which associates the application to run as follows in the Solution Explorer:

032513_1141_VisualStudi2.png


5.Now open the program.cs file to write the business logic. Here you can import namespace as per your requirements:

032513_1141_VisualStudi3.png


6.Place the code in the Main() entry point function to display some message over the console. We’ll use the WriteLine() method of the Console class to achieve this.

Code:
Please, Log in or Register to view codes content!

1.Finally, build the application using F9 or from the Build menu. If the application is error-free it will be successfully compiled.Here’s the output of this program:

032513_1141_VisualStudi4.png


Building Windows Form Applications using Visual Studio IDE

Windows Form applications are different from console applications in terms of user interface. In Windows Forms we can manipulate arbitrary controls such as buttons, textboxes, list box menu bars, image controls etc, and we don’t need to write code using these controls like when usingC/C++. Just simply select the specific control from the toolbox and drag it over the form.

1.Open the Visual Studio 2010 program.
2.Then go to File menu,click on Project and select Windows Forms Application. The following dialog box will open:

032513_1141_VisualStudi5.png


3.When the solution file process is finished, a Form is created with two version files, Form1.Designer.cs for designing the form with controls and Form1.cs for coding the corresponding controls.
4.Now open the Toolbox from the View menu and select your control, such as Button, and drag it over the form:

032513_1141_VisualStudi6.png


5.Now you can edit the button control properties such as name, text, size, position, color, style, etc., from the properties box:

032513_1141_VisualStudi7.png


6.Now create an Event handler for the button control such click, change, drag, enter etc., from the properties window.
7.Select the button control, open its properties and click on Event. Now double click the properties list box and a click event is generated in the code file as follows:

Code:
Please, Log in or Register to view codes content!


1.You can open the Form1.cs code file using View Code in the Solution Explorer:

032513_1141_VisualStudi8.png


2.Now put a code for functionality in the btnClick_Click() method so that when a user clicks the button, a message box will appear:

Code:
Please, Log in or Register to view codes content!

3.Finally,compile the code using F9.A form with a button control will appear, and when you the click the button,the following output is generated:

032513_1141_VisualStudi9.png


Even if you don’t import the message box class into the source,we can still compile this code by using the csc.exe:

csc /r:System.Windows.Forms.dll *.cs

Response Files

Sometimes you have to specify the complex number of input options at the command prompt to inform the compiler so that in can process the source code. A C# response file with a .rsp extension provides a mechanism to execute multiple instructions during compilation. Let’slook at an example .rsp file:

Code:
Please, Log in or Register to view codes content!

Save this file as myRespFile.rspin the same directory where the source files reside. Then open the command prompt and compile the test.exe files:

csc.exe @myRepFile

It is mandatory to put the ‘@’ symbol before the response file during compilation.We can also specify multiple response files using csc.exe as well.

032513_1141_VisualStudi10.png


Note:You can also create Windows Form applications using Notepad and compile them with csc.exe.

032513_1141_VisualStudi11.png


Anatomy of the Main() Method

Every executable program in the .NET framework must have a definition for the Main() method which is used to signify the entry point of the program. The Main() method signature is adorned with the static keyword which illustrates that this method automatically executes without creating the class instance. It has a default return type void which means that we don’t explicitly define the return value using the return keyword. It has a single parameter which happens to be an array of strings (string[] args) and it must be private, although that is not required.

Code:
Please, Log in or Register to view codes content!

There are four valid signatures for Main() when being used as the entry point:

Code:
Please, Log in or Register to view codes content!

Processing command line arguments

The following program sample loops through the string array, passes to the Main() method and writes the value of each option to the console window:

Code:
Please, Log in or Register to view codes content!

You can compile this program using the command prompt. When you run the executable file, you can pass in arguments after the name of the program as follows:

032513_1141_VisualStudi12.png


You can also test the command line parameter using Visual Studio instead of command line. Just go to the Solution Properties, open the Debug pane, and specify the arguments in Start Options as follows:

032513_1141_VisualStudi13.png


Multiple Entry Points

When a C# console or Windows application is compiled, the compiler by default looks for exactly one entry point (called Main() method). If there is more than one Main() method, the compiler issues an error message. Consider the following example where we are implementing more than one entry point:

Code:
Please, Log in or Register to view codes content!

This program contains two entry points in two classes. If you try to compile this code in the usual way, you will get the following error message:

032513_1141_VisualStudi14.png


However, you can still compile this code successfully by explicitly telling the compiler which of these methods to use as the entry point for the program using the Solution Property:

032513_1141_VisualStudi15.png


Here, if you notice in the Startup object, the compiler made an entry of both entry points. You have to explicitly choose which Main() method you want to execute.

Using Comments in Code

Comments are used for documentation purposes in the source code. .NET IDE caters to two kinds of comments, single-line (// ….) and multiple-line (/*…….*/). Everything in both comments will be ignored by the compiler.Here’s an example of single-line and multiple-line comments:

Code:
Please, Log in or Register to view codes content!

Summary

This article covered the areas needed to write simple C# programs using Visual Studio IDE or others. This article gave you an understanding to create your first application in C#.net; creating this example has helped you to explorer the new Visual Studio IDE and shown how powerful the features of the IDE are. It has also discussed other significant concepts such comment to embellish the code. You have also seen the program Entry Point implementation in diverse ways.


source:
Please, Log in or Register to view URLs content!
 
Top Bottom