Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Visual CSharp 2005 Express Edition (2006) [eng]

.pdf
Скачиваний:
42
Добавлен:
16.08.2013
Размер:
9.96 Mб
Скачать

Chapter 4

Figure 4-2

To get more comfortable with namespaces and how they work, you need to use them yourself. To do that, you will want to create another Windows project to work with.

Try It Out

Creating the Initial Project

While in C# Express:

1.Choose Project... from the File New menu task. The New Project dialog box appears.

2.Highlight Windows Application.

3.In the Name property, type in the name you want to call the project. You can see that Chapter4CheckoutNamespaces was the title given to this project in Figure 4-3.

4.Click OK. C# Express creates the project. The initial Windows form appears.

5.Click the plus sign by the References node in the Solution Explorer; you now see all the references to namespaces that are set by default (see Figure 4-4).

Figure 4-3

54

Introducing .NET

Figure 4-4

While it is great to see the namespaces listed for your project, it is even better if you know what you can do with them.

Working with .NET Namespaces

While some programming environments let you use libraries of code to enhance your programming, few actually use those libraries themselves. Every object that is created within C# Express uses namespaces. Even the form in the form designer has been created using .NET namespaces, but it is maintained by C# Express for you, so you can just drag and drop nicely using the editor. While I could have you open the file that makes up the design portion of the form, it wouldn’t really help you get used to working with .NET namespaces.

Object Browser: Tool of the Namespace Trade

One of the tools worthwhile to look at when you are learning about namespaces is the Object Browser. With the Object Browser you can search through and locate the syntax for various classes you want to use. Once you locate the class in the namespace, you can press F1 and get help on it if necessary.

55

Chapter 4

The Object Browser is a great tool when you just want to look through a namespace to get an idea of what is included.

Try It Out

Using the Object Browser

The best way to get comfortable with the Object Browser is to go ahead and use it. For this Try It Out, you will open the Object Browser and search for the MessageBox class. So, in the project you created earlier in this chapter:

1.Choose Object Browser from the View Other Windows menu choice, as shown in Figure 4-5.

Once in the Object Browser, you see all of the namespaces that are referenced in the current project. You even see an entry for the project itself, as shown in Figure 4-6.

2.In the entry that says <Search>, type in the term MessageBox, then press Enter. You will now see a list of classes that have the word MessageBox in them.

Figure 4-5

56

Introducing .NET

Figure 4-6

3.Highlight the entry that reads System.Windows.Form.MessageBox. All the methods of this class appear, in this case just the Show methods, as shown in Figure 4-7.

4.Highlight any of the entries, and press F1. You will then see the help for that particular method, as shown for the Show method in Figure 4-8.

Many of the methods and properties of classes can have more than one way of being called or set. This is called “overloading.” You can create your own classes that use overloading as well.

You can now close both the help screen and the Object Browser by clicking on the red X in the code of each.

There are a number of ways to use the .NET namespaces within your applications. One is by typing the full name of the namespace down to the class or method (action) you want use with the namespace. This is called using the fully qualified namespace.

57

Chapter 4

Figure 4-7

Figure 4-8

58

Introducing .NET

Supplying the Fully Qualified Namespace

This is a fancy term for what is really just performing more work than necessary. For example, if you want to use the MessageBox() method, which is in the System namespace, you would then use the following syntax:

System.Windows.Forms.MessageBox.Show(“Hey There”);

One of the nice features of C# Express and other Microsoft editors is that they provide a help feature called IntelliSense. While it is a dumb name, it is a great feature. IntelliSense actually builds your commands as you move down the qualified namespace, and then lets you know the possible arguments that can be passed to the method.

To give you a bit of experience working with fully qualified namespaces and even with using the IntelliSense feature, perform this next Try It Out.

Try It Out

Calling the MessageBox.Show() Method Using the Fully Qualified

 

Namespace

Using the project you created in the first Try It Out of this chapter:

1.Double-click on the Form1.cs. The code for Form1 appears, as shown in Figure 4-9.

Figure 4-9

59

Chapter 4

2.In between the open and close curly brackets, under the lines that read

private void Form1_Load(object sender, EventArgs e)

{

type the following:

System.Windows.Forms.messageb

As you move through each segment of classes, the editor capitalizes the name and then gives a list for the next segment. You can see this in Figure 4-10.

IntelliSense helps a great deal as you are writing out the statements or are not sure the path or syntax. To go to the next segment, press either the period or parenthesis as you get toward the end of the statement.

If you misspell or don’t press the period to complete the segment of the command, it will not get capitalized and will cause problems. Remember that C# is a case-sensitive language. If this happens, either capitalize the command yourself or erase the segment and start over.

3.Complete the rest of the command so that it reads

System.Windows.Forms.MessageBox.Show(“Hey There”);

The screen now appears as shown in Figure 4-11.

Figure 4-10

60

Introducing .NET

Figure 4-11

Using the fully qualified namespace is good when you have classes or methods that are similarly named. However, it can be somewhat of a hassle to have to keep typing out the full name, even using IntelliSense.

.NET provides another way to specify the full path once in the class you are currently working in: with the Using directive.

The program you just created displays a message box as the form is opened. To build and test this application, press F5.

The Using Directive

Instead of opening the form design file, I will show how to use the namespaces by opening the code portion of the form and showing you how .NET “hooks” in the namespaces for you. For example, if you open code for the form1 in your new project, you will see a region, also called code blocks, at the top made up of Using directives By default, you will see a list of namespaces that are included in each Windows form created. You can see this for the current project in Figure 4-12.

61

Chapter 4

Figure 4-12

By clicking the plus/minus symbols, you are either expanding or collapsing the region. There is quite a bit of code that is created and then placed in regions using both #region . . . #endregion statements and now automatically by the C# Express editor for you based on the code block being created. If you go into some of these regions, be careful, because the majority of the regions are code written by C# Express and should not be changed.

With the Using directive, you are telling C# that you are planning on using the specified namespace so you don’t have to type full syntax out. Remember, the namespaces shown in Figure 4-12 are there by default. As you get more advanced in your writing of code, you will add your own namespaces that you will find useful.

A cool tip if you are going to be using a class from a namespace specified in the Using Directives region is that you can press Ctrl+Space, and a list of available classes and methods is displayed using IntelliSense.

62

Introducing .NET

Try It Out

Calling the MessageBox.Show() Method with the Using Directive

Using the project you created in the previous Try It Out, erase the fully qualified MessageBox.Show command you created. With the current line in between the curly brackets, again:

1.

2.

Press Ctrl+Space. The IntelliSense list appears.

Type:

Messageb

The list then goes to the MessageBox class, as shown in Figure 4-13.

3.Finish the statement so it reads

MessageBox.Show(“Hey There Again”);

This is shown in Figure 4-14.

Figure 4-13

63

Соседние файлы в предмете Программирование на C++