Class5.DOC

(4785 KB) Pobierz
Beginning Visual C# Express

5-35



Labels, Text Boxes, Variables

 

Beginning Visual C# Express

 

 

5. Labels, Text Boxes, Variables

 

 

Review and Preview

 

              We continue our look at the Visual C# Express environment and learn some new controls and new C# statements.  As you work through this class, remember the three steps for building a Visual C# Express project:  (1) place controls on form, (2) assign properties to controls, and (3) write event methods.  In this class, you will examine how to find and eliminate errors in your projects, learn about the label and text box controls, and about C# variables.  You will build a project that helps you plan your savings.

 


Debugging a Visual C# Express Project

 

              No matter how well you plan your project and no matter how careful you are in implementing your ideas in the controls and event methods, you will make mistakes.  Errors, or what computer programmers call bugs, do creep into your project.  You, as a programmer, need to have a strategy for finding and eliminating those bugs.  The process of eliminating bugs in a project is called debugging.  Unfortunately, there are not a lot of hard, fast rules for finding bugs in a program.  Each programmer has his or her own way of attacking bugs.  You will develop your ways.  We can come up with some general strategies, though, and that’s what we’ll give you here.

 

              Project errors, or bugs, can be divided into three types:

 

·         Syntax errors

·         Run-time errors

·         Logic errors

 

Syntax errors occur when you make an error setting a property in design mode or when typing a line of C# code.  Something is misspelled or something is left out that needs to be there.  Your project won’t run if there are any syntax errors.  Run-time errors occur when you try to run your project.  It will stop abruptly because something has happened beyond its control.  Logic errors are the toughest to find.  Your project will run OK, but the results it gives are not what you expected.  Let’s examine each error type and address possible debugging methods.


Syntax Errors

 

              Syntax errors are the easiest to identify and eliminate.  The Visual C# Express program is a big help in finding syntax errors.  Syntax errors will most likely occur as you’re setting properties for the controls or writing C# code for event methods.

 

              Start a new project in Visual C# Express.  Go to the project window and try to set the form Width property to the word Junk.  (Click the plus sign next to the Size property to see Width.)  What happened?  You should see a little window like this:

 

 

Click Details and you will see an explanation of the problem.  Remember that property values must be the proper type.  Assigning an improper type to a property is a syntax error.  But, we see Visual C# Express won’t let us make that mistake.  Click Cancel to restore the Width to what it was before you tried to change it.

 


              What happens if you cause a syntax error while writing code.  Let’s try it.  Establish a Form1_Load event method using the properties window (recall: choose Events button in properties window, scroll down to Load event and double-click the event name)When the code window opens, under the left curly brace following the header line, type this line, then press <Enter>:

 

this.BackColor 0 Color.Red;

 

This would happen if you typed 0 instead of = in the assignment statement.  What happened?  In the code window, part of the line will appear underlined with a squiggle, similar to what Microsoft Word does when you misspell a word:

 

 

Visual C# Express has recognized that something is wrong with this statement.  You should be able to see what.  Any line with an error will be ‘squiggled’ in places. Placing the cursor over a squiggled line will give some indication of your error.

 

              So, if you make a syntax error, Visual C# Express will usually know you’ve done something wrong and make you aware of your mistake.  The on-line help system is a good resource for debugging your syntax errors.  Note that syntax errors usually result because of incorrect typing - another great reason to improve your typing skills, if they need it.


Run-Time Errors

 

              Once you successfully set control properties and write event methods, eliminating all identified syntax errors, you try to run your project.  If the project runs, great!  But, many times, your project may stop and tell you it found an error - this is a run-time error.  You need to figure out why it stopped and fix the problem.  Again, Visual C# Express and on-line help will usually give you enough information to eliminate run-time errors.  Let’s look at examples.

 

              Working with the same example as above, try to run the project with the incorrect line of code.  After you click the Start button on the toolbar, the following window should appear:

 

 

This tell us an error has occurred in trying to ‘build’ the project.  Click No – we don’t want to continue.  We want to find the error.  If ‘build errors’ occur, they are listed in another Visual C# Express window – the Error List.  This list shows you all errors detected in trying to run your program.  Go to that window now (if it’s not there already, choose View in menu, select Error ListError List appears in the Design window.  Yours might be floating or docked somewhere. 

 


My Error List window is:

 

 

It has two errors that must be cleared before the program will run.  Note each error refers to Line 23 on Form1, pointing to the offending line.  Double-click the first error to move to the corresponding line – the error should be highlighted:

 

 


Visual C# Express is telling you that there is something wrong with how you used the 0.  Using the hints from the task list (the code expected an end-of-line indicator), you should be able to see that the assignment operator (=) is missing.  If you don’t see the problem, clicking <F1> might give you more help. 

 

              Let’s say we corrected our error by adding the = sign, but we accidentally left out the letter ‘k’ in the BackColor property name, or we typed:

 

this.BacColor = Color.Red;

 

Try running the project and you’ll see another ‘build error’ window.  Choose not to continue and go to the Error List:

 

 

The message again points to Line 23, saying ‘BacColor’ is not defined by the form.  Again, go to Line 23 (double-click the error message) in the code window and you should see:

 


The word BacColor will be highlighted.  Visual C# Express is telling you it can’t find this property for the particular control (the form).  You should note the misspelling and correct it.

 

              Now, let’s say you correct the property name, but mess up again and type thiss instead of this when referring to the form:

 

thiss.BackColor = Color.Red;

 

Run the project.  You will get another build error, choose not to continue and view the Task list:

 

 

The key message here is ‘thiss does not exist …’  This usually appears when you have misspelled the assigned name of a control in C# code.  Visual C# Express is trying to assign a property to something using the ‘dot notation’:

 

controlName.PropertyName = Value;

 

But, it can’t find a control with the given name (thiss in this case).  Go to the code window, correct the error and run the application.  You should finally get a red form!!

 

              The errors we’ve caused here are three of the most common run-time errors:  misspelling an assigned Name property, misspelling a property name, or leaving something out of an assignment statement.  Notice each run-time error seen was detected prior to running, resulting in a build error.  There are other run-time errors that may occur while your application is actually running.  Let’s look at one example.

 

              In the code window, after the corrected line to set the form red, type these lines (don’t worry about what these mean just now):

 

int a = 0;

this.Width = this.Width / a;

 

We are replacing the form width by its present value divided by zero (the variable a)!  Yes, I know you can’t divide a value by zero and that’s the error we’re trying to detect.  Notice Visual C# Express lets you type the line without pointing out a mistake.  Run the project.  Run the project.  You will not get a build error, but this window will appear:

 

 

Visual C# Express refers to some run-time errors as exceptions.  This message box says you have an ‘DivideByZero’ exception.  Note the offending line is highlighted in the code window and suggestions for fixing the problem are listed under Troubleshooting Tips.  At this point, it should be obvious what is wrong with this line of code.  You can stop the program and fix the error.  Follow this same procedure should you ever encounter a similar error when running a project.  If a window appears saying you have some kind of exception, the line with the detected error will be shown with suggestions for fixing the error.  Be sure to stop the program before trying to fix the error.

 

We’ve seen a few typical run-time errors.  There are others and you’ll see lots of them as you start building projects.  But, you’ve seen that Visual C# Express is pretty helpful in pointing out where errors are and on-line help is always available to explain them.  One last thing about run-time errors.  Visual C# Express will not find all errors at once.  It will stop at the first run-time error it encounters.  After you fix that error, there may be more.  You have to fix run-time errors one at a time.

 


Logic Errors

 

              Logic errors are the most difficult to find and eliminate.  These are errors that don’t keep your project from running, but cause incorrect or unexpected results.  The only thing you can do at this point, if you suspect logic errors exist, is to dive into your project (primarily, the event methods) and make sure everything is coded exactly as you want it.  Finding logic errors is a time-consuming art, not a science.  There are no general rules for finding logic errors.  Each programmer has his or her own particular way of searching for logic errors. 

 

              With the example we have been using, a logic error would be setting the form background color to blue, when you expected red.  You would then go into the code to see why this is happening.  You would see the color Color.Blue instead of the desired value Color.Red.  Making the change would eliminate the logic error and the form will be red.

 

              Unfortunately, eliminating logic errors is not as easy as this example.  But, there is help.  Visual C# Express has something called a debugger that helps you in the identification of logic errors.  Using the debugger, you can print out properties and other values, stop your code wherever and whenever you want, and run your project line-by-line.  Use of the debugger is an advanced topic and will not be talked about in this course.  If you want to improve your Visual C# Express skills, you are encouraged to eventually learn how to use the debugger.

 

              Now, let’s improve your skills regarding Visual C# Express controls.  We’ll look at two new controls:  the label and the text box.

 


Label Control

 

              A label is a control that displays information the user cannot edit directly.  It is most often used to provide titles for other controls.  Or, it is used to display the results of some computer operation.  The label control is selected from the toolbox.  It appears as:

 

              In Toolbox:              On Form (default properties):



                           

 

Properties

 

              A few useful properties for the label are:

 

Property              Description

Name              Name used to identify label.  Three letter prefix for label names is lbl.

Text              Text (string type) that appears in the label.

TextAlign              Specifies how the label text is positioned.

Font              Sets style, size, and type of Text text.

BackColor              Sets label background color.

ForeColor              Sets color of Text text.

Left               Distance from left side of form to left side of label (referred to by X in properties window).

Top               Distance from top side of form to top side of label (referred to by Y in properties window).


Width               ...

Zgłoś jeśli naruszono regulamin