Professional Documents
Culture Documents
Processing Data
Processing Data
Chapter 3
Processing Data
Topics
• 3.1 Reading Input with TextBox Controls
• 3.2 A First Look at Variables
• 3.3 Numeric Data Type and Variables
• 3.4 Performing Calculations
• 3.5 Inputting and Outputting Numeric Values
• 3.6 Formatting Numbers with the ToString Method
• 3.7 Simple Exception Handling
• 3.8 Using Named Constants
• 3.9 Declaring Variables as Fields
• 3.10 Using the Math Class
• 3.11 More GUI Details
• 3.12 Using the Debugger to Locate Logic Errors
productLabel = productDescription;
MessageBox.Show(productDescription);
string message;
Message = "Hello " + "world";
12 + " apples";
"Total is " + 25.75;
string employeeID;
employeeID = 125;
• Multiple variables with the same type may be declared with one
statement
Or
double temperature = 87.6;
int wholeNumber;
decimal moneyNumber = 4500m;
wholeNumber = (int) moneynumber;
double realNUmber;
decimal moneyNUmber = 625.70m;
realNumber = (double) moneyNumber;
int x = 7, y = 3;
MessageBox.Show((x / y).ToString());
int x = 7, y = 3;
MessageBox.Show(((double) x / (double) y).ToString());
• Examples:
try { }
catch { }
• The try block is where you place the statements that could have exception
• The catch block is where you place statements as response to the
exception when it happens
miles = double.Parse(milesTextBox.Text);
catch (Exception ex)
gallons = double.Parse(gallonsTextBox.Text);
{ mpg = miles / gallons;
MessageBox.Show(ex.Message); mpgLabel.Text = mpg.ToString();
} }
catch
{
MessageBox.Show("Invalid data was entered.");
}
Copyright © 2017 Pearson Education, Ltd.
3.8 Using Named Constants
• A number constant is a name that represents a value
that cannot be changed during the program's execution
• In C# a constant can be declared by const keyword
ControlName.Focus();