• Không có kết quả nào được tìm thấy

Using the .NET SDK

Trong tài liệu in .NET (Trang 37-41)

Understanding the Internet and Network Programming

1.11 Using the .NET SDK

16 1.11 Using the .NET SDK

applications may be subject to restrictions; for instance, when they are exe-cuted from a network share, they could be prevented from operating. On the whole, though, if a component can do the job, use it.

The Internet Explorer component is a Common Object Model (COM) control. This type of model was used most extensively in Visual Studio 6.0.

When a COM object is imported into a .NET application, a Runtime call-able wrapper (RCW) class is created. This class then exposes all the proper-ties and methods of the COM object to .NET code. In some cases, this importing process produces an interface that is virtually identical to the original COM object; however, as aptly demonstrated in the previous example, there may be some differences in the syntax of function calls.

In the original COM object, the Navigate method’s last four parameters were optional, but in the case of C#, the optional parameters had to be passed ref notUsed.

1.11 Using the .NET SDK 17

Windows forms applications from the command line. The previous exam-ple, although easy to implement in Visual Studio .NET, would require a large and complex program. Nevertheless, it should be informative to Visual Studio .NET developers to be aware of the code that is autogener-ated by Visual Studio .NET.

In the true programming tradition, we shall start with a program that simply displays “Hello World.” To make this different, the program will be written as a Windows form. After all, DOS console applications are very much past their sell-by date, and there seems little point in using them at all.

The code for this application may seem daunting at first, but this should illustrate how much extra work is required to implement applications with-out Visual Studio .NET.

First, decide which language you want to develop in, either C# or Visual Basic .NET. Open a text editor, such as Notepad, and type in the following code:


using System;

using System.Windows.Forms;

namespace helloWorld {

public class Form1 : System.Windows.Forms.Form {

public Form1() {

this.Text = "Hello World";



static void Main() {

Application.Run(new Form1());

} } }


Imports System

Imports System.Windows.Forms Public Class Form1

Inherits System.Windows.Forms.Form

Public Sub New ( ) InitializeComponent( ) End Sub

Private Sub InitializeComponent( ) Me.Text = "Hello World"

End sub End Class Module Module1 Sub Main ( )

Application.Run ( new Form1 ( ) ) End sub

End Module

All this code does is open a window with the caption “Hello World,”

which is somewhat underwhelming for the amount of code entered. Look-ing closely at the code, you can see the process of events that make up a Windows application in .NET.

An application in .NET is made up of namespaces, some of which are system defined and others are coded in. This application contains three namespaces: System, System.Windows.Forms, and helloWorld. The latter is the only namespace of the three that is actually supplied by the program-mer. The helloWorld namespace contains a class, named Form1. This class inherits from System.Windows.Forms.Form. This means that the class will have a visible presence on screen.

Whenever a class is created, a function known as the constructor is called.

This function can be recognized in C# when the name of the function is the same as that of the class. In VB.NET, the constructor is a subroutine named New. In the case of the previous example and in most Windows applications, this constructor is used to place user interface elements (some-times referred to as widgets) on the form. In the previous example, the con-structor calls InitializeComponent, which then sets the window name of the current form (this) to “Hello World.”

Every application must have a starting point. It is tradition in virtually every programming language that the stating point should be called Main. In C#, the [STAThread] attribute indicates the function which acts as the entry point for this single threaded apartment (STA) application. Every application must have one, and only one, entry point.

[STAThread] static void Main()

In VB.NET, the main function is coded in a different way but operates identically. The main function must appear in a separate module and be coded as follows. A module is a programmatic element that contains code that is global to the entire application.

Module Module1: Sub Main ( )

Once a Windows application starts, at least one form (a class inheriting from System.Windows.Forms.Form) must be created in order for there to be a visual interface. To create a new form, we call Application.Run, passing an instance of the form.

1.11.1 Compiling with Visual Basic.NET

Save the file to d:\temp\helloworld.vb. Open the command prompt by pressing Start→→Run and then typing →→ cmd for Windows NT, 2000, or XP or

command for Windows 95, 98, or ME.

Note: Path names mentioned differ among computers, depending on installation options.

Type the following:


D:\temp> path %path%;C:\WINDOWS\Microsoft.NET\Framework\v1.0.3705 D:\temp> Vbc /t:winexe /r:system.dll /r:system.windows.forms.dll helloworld.vb

D:\temp> helloworld

Figure 1.7

“Hello World”


1.11.2 Compiling with C#

Save the file to d:\temp\helloworld.cs. Open the command prompt by pressing Start > Run and then typing cmd for Windows NT, 2000, or XP or

command for Windows 95, 98, or ME.

Note: Path names mentioned differ among computers, depending on installation options.


D:\temp> path %path%;C:\WINDOWS\Microsoft.NET\Framework\v1.0.3705 D:\temp> csc /t:exe helloworld.cs

D:\temp> helloworld

1.11.3 Testing the application

To run the application, you need to compile it first. Depending on what language you used to program the application, skip to the relevant section.

Once it has compiled, you can run the application by clicking on the exe-cutable (.exe) file generated from the compilation. You should see a form resembling Figure 1.7.

Trong tài liệu in .NET (Trang 37-41)