Monday, July 26, 2010

Visual C# 2005 Quick Start of the while statement

Use while statements can be true in a Boolean expression is subject to repeat the run a statement.
while statement syntax is as follows:
while (booleanExpression)

statement


First, the Boolean expression will be evaluated, if it is true, on the run statement, then the Boolean expression is evaluated again. If the expression is still true, to run the statement again, and again the expression is evaluated. This process will continue repeatedly until the boolean expression evaluates to false; time, while statement will exit, and from the first statement after the while continue. while statement syntax with if statements have many similarities (in fact, in addition to the two different keywords, the syntax is exactly the same):

expression must be a Boolean expression.

Boolean expression must be placed within parentheses.

If the first value, the Boolean expression is false, statement is not run.

If you want to in a while under the control of the implementation of two or more statements, you must use braces to group into a block statement.

The following while statement is written to the console the value 0 to 9:

int i = 0;
while (i != 10)
(
Console.WriteLine (i);
i++;
)

All the while statement should be terminated at some point. Novice common mistake is to forget to add a special statement, it would eventually result in a Boolean expression evaluates to false and terminate the cycle. In the example, i++; on this situation.

Note that while loop variable i controls the final number of cycles. This is a very popular representation, has the effect of the variable is sometimes called the sentinel variable (Sentinel variable).

In the following exercise, preparing to write a while loop, it is time to read from a source file line content, and write each line of a text box.

use the while statement

1. WhileStatement open Visual Studio 2005 project, which is located in My Documents folder under the Microsoft PressVisual CSharp Step by StepChapter 5WhileStatement subfolders.

2. Select "Debug" | "to start (not debugging)."

Visual Studio 2005 to build and run the Windows application. Application itself is a simple text file viewer that allows you to select a file to display its contents.

3. Click the "Open File" (Open File) button.

Then there are "Open" dialog box

4. Switch to the My Documents folder under the Microsoft PressVisual CSharp Step by Step Chapter 5WhileStatementWhileStatement subfolders.

5. Select the Form1.cs file, and then click "Open."

Form1.cs file name will be displayed in small text box, but the contents of the file does not display in the large text box. This is because we have not achieved the appropriate code to read the contents of the source file and display those in the large text box contents. The following steps will add this feature.

6. Close the form, return to Visual Studio 2005.

7. In the "Code and Text Editor" window displays the file Form1.cs code, find openFileDialog_FileOk method.

Users in the "Open" dialog box, select a file, and click "Open" button, call the method. Method is currently the subject:
string fullPathname = openFileDialog.FileName;

FileInfo src = new FileInfo (fullPathname);
filename.Text = src.Name;

/ Bin / boot / dev / etc / home / lib / lost + found / media / misc / mnt / net / opt / proc / root / sbin / selinux / srv / sys / tmp / u01 / usr / var / vmware add while loop here backup / bin / conf / data / log / maint / svn / tmp /


The first statement declares a string variable, named fullPathname, and it is initialized to openFileDialog object FileName property. The statement fullPathname initialized to "Open" dialog box, select the source file's full name (including path).

Note openFileDialog object is from the "toolbox" of selected components of an instance of the OpenFileDialog. This component provides the methods used, you can display to the user a standard Windows "Open" dialog box, allowing users to choose a file, and get selected file name and path.

No comments:

Post a Comment