C Sharp (C#) : Benadir University
C Sharp (C#) : Benadir University
C Sharp (C#) : Benadir University
Course: C#
Class: Batch13
Chapter Three
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.1 Reading Input with TextBox
Control
• TextBox control
– a rectangular area
– can accept keyboard input from the user
– locates in the Common Control group of the
Toolbox
– double click to add it to the form
– default name is textBoxn
where n is 1, 2, 3, …
The Text Property
• A TextBox controls Text property stores
the user inputs
• Text property accepts only string values,
e.g.
textBox1.Text = “Hello”;
• To clear the content of a TextBox control,
assign an empty string(“”)
textBox1.Text = “”;
3.2 A First Look at Variables
• A variable is a storage location in memory
• Variable name represents the memory
location
• In C#, you must declare a variable in a
program before using it to store data
• The syntax to declare variables is:
DataType VaraibleName;
Data Types
• A C# variable must be declared with a
proper data type
• The data type specifies the type of data a
variable can hold
• C# provides many data type known as
primitive data types
– they store fundamental types of data
– such as strings and integers
Variable Names
• A variable name identifies a variable
• Always choose a meaningful name for
variables
• Basic naming conventions are:
– the first character must be a letter (upper or
lowercase) or an underscore (_)
– the name cannot contain spaces
– do not use C# keywords or reserved words
String Variables
• String is a combination of characters
• A variable of the string data type can hold any combination of characters,
such as names, phone numbers, and social security numbers
• Value of a string variable is assigned on the right of = operator surrounded
by a pair of double quotes:
productLabel.text = productDescription;
MessageBox.Show(productDescription);
String Concatenation
• Concatenation is the appending of one string to the end of another
string
• C# uses + operator for concatenation:
string message;
Message = “Hello “ + “world”;
12 + “ apples”;
“Total is “ + 25.75;
Declaring Variables Before Using
Them
• You can declare variables and use them later
private void showNameButton_Click(object sender, EventArgs e)
{
// Declare a string variable to hold the full name.
string fullName;
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;
3.4 Performing Calculations
int x = 7, y = 3;
MessageBox.Show((x / y).ToString());
int x = 7, y = 3;
MessageBox.Show(((double) x / (double) y).ToString());
3.5 Inputting and Outputting
Numeric Values
• Input collected from the keyboard are considered combinations of
characters (or string literals) even if they look like a number to you
• A TextBox control reads keyboard input, such as 25.65. However,
the TextBox treats it as a string, not a number.
• In C#, use the following Parse methods to convert string to numeric
data types
– int.Parse
– double.Parse
– decimal.Parse
• 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
Throwing an Exception
• In the following example, the user may entered invalid data (e.g.
null) to the milesText control. In this case, an exception happens
(which is commonly said to “throw an exception”).
• The program then jumps to the catch block.
• You can use the following try
{
to display an exception’s double miles;
double gallons;
default error message: double mpg;
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."):
}
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
Questions?