Read Microsoft Visual C# 2005 Express Edition: Build a Program Now! Online

Authors: Patrice Pelland

Tags: #General, #Computers, #C♯ (Computer program language), #Programming Languages, #C#, #Microsoft .NET Framework, #Computer Books: Languages, #Computer Graphics, #Application software, #C# (Computer program language), #Programming, #Microsoft Visual C# .NET, #Microsoft Visual C♯ .NET, #Electronic books, #Game Programming & Design, #Computing: Professional & Programming, #C (Computer program language), #Computers - Languages, #Programming Languages - C#, #Programming & scripting languages: general

Microsoft Visual C# 2005 Express Edition: Build a Program Now! (17 page)

BOOK: Microsoft Visual C# 2005 Express Edition: Build a Program Now!
2.32Mb size Format: txt, pdf, ePub
ads

OOP Terminology

and any other items in your applications can be updated to meaningful names using the
Although this book is not

an OOP book, we’ll certainly

renaming symbol functionality.

use many of those constructs in

the real application we’ll build.

What Can You Rename?

We've talked about OOP in

Chapter 1 and Chapter 4, but if

So far in the test project we’ve worked in, we have not paid attention to the controls' names
you want to learn more about

because we didn’t have to write much code and because the project was a quick
this paradigm, you can go back

to the Start Page. If it’s not vis-

prototype to test new features. At this point, our controls are all named something like
ible, just click the View menu,

textBox1, textBox2, label1, and so on. That’s okay for what we’ve been doing, but when you
Other Windows, and then select

develop full applications, you always want to give meaningful names to your controls and
Start Page. In the Start Page,

variables so that your code becomes self-documented and easier to read and maintain.
click on the How do I . . .?

hyperlink in the Getting Started

section. Once you’re in the

How and Where to Use the Rename Feature

help system, use the Contents

pane to navigate to Visual C#

So far in our test project, we have defined some text boxes, labels, and other controls, but
Express\C# Language Primer

they all have their autogenerated names (e.g., textBox1 or label1). We can use the rename
(Visual C# Express)\Classes.

feature to give meaningful names to these controls. The renaming feature can be used from
Click and read whenever you

three different places within the IDE: in the Properties window, directly in the code, and in
want to learn more about OOP

the Solution Explorer.

and the Visual C# language. It’s

not a complete OOP book, but

The first place we can use the renaming feature is in the Properties window at design
it’s more than enough for what

time. So far in our test project, we’ve used the form name Form1. In the next exercise, we'll
we are doing in this text.

rename Form1 to TestProjectForm. The expectation is that this change gets propagated throughout the code in the project. But just to see how the functionality works, we’ll look into all files where the Form1 symbol is used.

Chapter 5: Creating Your First Full Windows Application

67

C05.indd 67

C05.indd 67

10/24/05 3:15:45 PM

10/24/05 3:15:45 PM

TO USE THE RENAME FEATURE

This is a good point to intro-

1 In Solution Explorer, expand the form1.cs file by clicking the plus sign (+) beside Form1.cs.
duce a new button from the

Solution Explorer toolbar. The

Show All Files button looks like

Right-click the file name
Form1.Designer.cs
and then select
View Code
. Look at Figure 5-13 for a 2 visual representation of what you should see.

this in the toolbar:

The Show All Files enables

you to see files that are part

of the project but that you do

not need to work with in most

Figure 5-13

circumstances. For instance the

output files of your application.

They are important but not in

Now let me introduce you to a useful feature: Search. With the Form1.Designer.cs source code in the 3 code editor, press
Ctrl+F
and then type
Form1
in the Find What text box. Before you click the Find
the context of Visual Studio.

Next button, make sure to select
Current Project
from the Look in list box. Figure 5-14 shows how the search should be configured.

Figure 5-14

Search for all occurrences of Form1 in the code by clicking the
Find Next
button. You should see that 4 the search goes through three different files:
Form1.cs
,
Program.cs
, and
Form1.Designer.cs
. Once all instances of Form1 have been found, a dialog box will display a message saying that the search is complete and that there are no more occurences left based on your search criteria. Now that all instances of Form1 have been identified, we can rename it
TestProjectForm
. To do that, 5 select the
Form1.cs [Design]
tab
to return to the designer surface. Then click the title bar to select the form.

68

Microsoft Visual C# 2005 Express Edition: Build a Program Now!

C05.indd 68

C05.indd 68

10/24/05 3:15:46 PM

10/24/05 3:15:46 PM

In the Properties window, be sure the form control named
Form1
is selected. Modify the (Name) 6 property by changing
Form1
to
TestProjectForm
. Press
Enter
to begin renaming. A small hourglass should be displayed while the renaming is in process.

Now repeat the search from Steps 3 and 4 and you’ll see that the only occurrence left is a string that 7 corresponds to the Form control text property (i.e., title bar name). You can also rename a symbol directly in the code. In Form1.cs, place the cursor anywhere in the 8 word
textBox1
in the following line of code:

this.textBox1.Text = “HelloWorld”;

Right-click and select
Refactor
and then
Rename
; a dialog box like the one in Figure 5-15a appears. 9 Replace
textBox1
with
txtMessage
; the dialog box should now look like the one in Figure 5-15b. You have three check boxes that can control how deep your rename will go.

Figure 5-15

10 Click
OK
to start replacing all occurences of
textBox1
with txtMessage. You should see a dialog box that will give you a preview of all the changes that will be done by the Refactoring. The upper section shows you where the string was found and the bottom portion shows you what the change will look like if applied to the selected item. The check boxes allow you to select only the changes you really want to perform. The neat thing is that they are organized by type of change: one for the code elements, one for the comments, and finally one for the strings. Look at Figure 5-16a to see the Preview Changes dialog box upper window completely scrolled and Figure 5-16b for the first selected change preview.
Chapter 5: Creating Your First Full Windows Application

69

C05.indd 69

C05.indd 69

10/24/05 3:15:47 PM

10/24/05 3:15:47 PM

Figure 5-16

Click Apply to perform the renaming. It's that easy. Imagine how much time you would save using the Rename feature if you had 10 files with hundreds of lines of code. Not only would you be sure to find every occurrence, the code would be a lot easier to read.

BOOK: Microsoft Visual C# 2005 Express Edition: Build a Program Now!
2.32Mb size Format: txt, pdf, ePub
ads

Other books

Out of My Element by Taryn Plendl
An Imprudent Lady by Elaine Golden
The Mountain Can Wait by Sarah Leipciger
Khan by Kathi S. Barton
Xtreme by Ruby Laska