Like this presentation? Why not share!

# Spf Chapter4 Variables

## by Hock Leng PUAH, Senior Lecturer at ITE College Central on Apr 20, 2010

• 503 views

> Variables in details ...

> Variables in details
. Number – byte, int and long
. Decimal – float and double
. Character
. Boolean
. String
> Implicit and explicit conversion
> Numbers, Decimals and their operations (=, +, -, *, / and others)
> Strings and operations (assigning value, concatenation)

### Views

Total Views
503
Views on SlideShare
444
Embed Views
59

Likes
0
2
0

### 8 Embeds59

 http://mpgddspf.blogspot.com 44 http://programmingex.blogspot.sg 4 http://www.mpgddspf.blogspot.com 3 http://programmingex.blogspot.in 3 http://programmingex.blogspot.ie 2 http://www.slideshare.net 1 http://mpgddspf.blogspot.it 1 http://programmingex.blogspot.com 1
More...

### Categories

Uploaded via SlideShare as Microsoft PowerPoint

## Spf Chapter4 VariablesPresentation Transcript

• Chapter 4
C# .NET: Variables
• Variables: value and reference types
Recall what we covered in week 1 using pictures to explain the difference between value types and reference types. This chapter will cover value types in detail
• Chapter 4 topics
Variables in details
Number – byte, int and long
Decimal – float and double
Character
Boolean
String
Implicit and explicit conversion
Numbers, Decimals and their operations (=, +, -, *, / and others)
Strings and operations (assigning value, concatenation)
• Value reference types
Number
Types not covered: short, unsigned number such as ulong (unsigned long integer), etc
Decimal
Type not covered: decimal (12 bytes)
• How to use them - Numbers?
byte numberInByte1 = 64; // OK: auto conversion
byte numberInByte2 = 256; // Error
intnumberInByte = 64; // Default is Integer
long numberInLong1 = 64L; // L for long number
long numberInLong2 = 64 // OK: auto conversion
• How to use them - Numbers?
byte numberInByte1 = 64; // OK: auto conversion
byte numberInByte2 = 256; // Error
intnumberInByte = 64; // Default is Integer
long numberInLong1 = 64L; // L for long number
long numberInLong2 = 64 // OK: auto conversion
Why cannot use small letter L ( l )?
• How to use them - Numbers?
byte numberInByte1 = 64; // OK: auto conversion
byte numberInByte2 = 256; // Error
intnumberInByte = 64; // Default is Integer
long numberInLong1 = 64L; // L for long number
long numberInLong2 = 64 // OK: auto conversion
// Default type is Integer
If a data is given as 64 without L => data is an integer. Since numberInLong1 is large enough to store integer 64, there is an auto (implicit) conversion.
Auto (implicit) conversion for numberInByte1 but error for numberInByte2
• Declare and initialize
// Declare and initialize one variable in one line
int number1 = 5; // number1 variable is
// assigned with a value 5
int number2 = 10;
// Declare and initialize more than one variable
// in one line
int number1 = 5, number2 = 10;
• Declare and initialize
// Declare and initialize one variable in one line
int number1 = 5; // number1 variable is
// assigned with a value 5
int number2 = 10;
// Declare and initialize more than one variable
// in one line
int number1 = 5, number2 = 10;
When a variable is declared and set with a value at the same time => declare and initialize
When it is set with value subsequently => assign with value
• How to use them - decimals?
float numberInFloat1 = 8.0F; // F for floating number
float numberInFloat2 = 8.0f; // OK: Small letter F
double numberInDouble1 = 8.0; // Default
double numberInDouble2 = 8.0f; // OK: Auto conversion
• How to use them - decimals?
float numberInFloat1 = 8.0F; // F for floating number
float numberInFloat2 = 8.0f; // OK: Small letter F
double numberInDouble1 = 8.0; // Default
double numberInDouble2 = 8.0f; // OK: Auto conversion
Do you think the following is OK? Why?
float numberInFloat3 = 8.0;
//Hint: Default is double
• Try it out!
• Create a new WinForm project: SpfChapter4
• Drag and drop a button onto the form
• Double click on the button and add the codes to button1_Click event:
/* Number Type */
byte numberInByte1 = 64; // OK: auto conversion
byte numberInByte2 = 256; // Error: Too big for byte
intnumberInByte = 64; // Default is Integer
long numberInLong1 = 64L; // L for long number
long numberInLong2 = 64 // OK: auto conversion
long numberInLong3 = 64l; // Error: Small letter L
int n1 = 5, n2 = 10; // Declare more than one var
// Next page
• Try it out!
/* Decimal Type */
float numberInFloat1 = 8.0F; // F for floating number
float numberInFloat2 = 8.0f; // OK: Small letter F
double numberInDouble1 = 8.0; // Default
double numberInDouble2 = 8.0f; // OK: Auto conversion
// Default for decimal is double (8 bytes)
float numberInFloat3 = 8.0; // Error: Too small (4 bytes )
// to store the double
// precision (accuracy)
• Explicit conversion
// Implicit conversion => Automatic conversion
// Explicit conversion => do it explicitly
// i.e. tell the compiler that you want it to convert and
// that you know what you are doing
float numberInFloat4 = (float) 8.0; // No more error
• Explicit conversion
// Implicit conversion => Automatic conversion
// Explicit conversion => do it explicitly
// i.e. tell the compiler that you want it to convert and
// that you know what you are doing
float numberInFloat4 = (float) 8.0; // No more error
// But still give error if the value is too big
// For floating type: max value is 3.4e38
float numberInFloat5 = (float) 3.5e38; // Error
• Value reference types
Other common value reference types
• Special type
String
string is a reference type but behaves like value type
Memory usage is reference type
Behave like value type
string str = “a new string”; // No need to use New keyword
Reason: Microsoft wants to make string in .NET safe and fast for programmer to handle sequence of characters.
Good tutorial on C# string: http://alturl.com/r4qa
• How to use them?
boolisMoving = true; // Use true or false
boolhasCompleted = false;
char answer = ‘ Y ’; // Between ‘ ’
string str = “my name”; // Between “ ”
• Try it out!
• Continue from previous project and add the codes to button1_Click event:
boolisMoving = true; // Boolean use true or false
boolhasCompleted = false;
char answer = 'Y'; // Between ‘ ’
string str = "my name"; // Between “ ”
• There is a specific relationship between where a variable is defined and where it can be used.
This is known as the scope of the variable.
apple only exists in Class2V while june only exists in Class2W. mrPuahexists in CampusMP, Class2V and Class2W.
CampusMP
mrPuah
Scope of variable
Class2V
apple
Class2W
june
• Scope of variable
A variable once declared, exist only within the code block : { .. }
button1_click( .. )
{
string apple = “ABC”; // declared here: apple only
// exist here
}
button2_click(.. )
{
apple = “DEF”; // Error: apple not defined
}
• Scope of variable
string mrPuah = “I am here!”; // Declared on
// outer { .. }
button1_click( .. )
{
mrPuah = “ABC”; // OK
}
button2_click(.. )
{
mrPuah = “DEF”; // OK
}
• Try it out!
• Continue from previous project and add the codes to button1_Click event:
string str = "my name"; // Previous code
{ // Add an inner code block
str = "change name"; // No error, within inner { .. }
}
str2 = "change name"; // Error: str2 only exist in
// inner { .. }
• Operators
Symbol to perform on expression (part of a statement)
For numbers and decimals: =, +, -, *, /, %, ++, -- and
+=, -=, *=, /=
For string: =, + (concatenate) and +=
For the full list of operators, refer to:
http://alturl.com/bokx
• Operators for numbers/decimals
• Operators for numbers/decimals
For complex expression like w + x / y - z
Use brackets ( .. ) to tell compiler which portion to evaluate first. Eg (w + x) / (y – z)
Otherwise, compiler will use operators precedence rule. Acronyms like BPODMAS
Refer to: http://alturl.com/9b8r
• Operators for numbers/decimals
• Operators for string
• Exercise 4.1
Textbook from page 52 – 71:
Part 1 String Variables in C#.NET
Part 2 Assigning Text to a String Variable
Part 3 Concatenation in C#.NET
• Exercise 4.2
Textbook from page 71 – 83:
Part 5 Integer Variables
Part 6 Double and Float Variables
Part 7 Double Variables in C# .NET
• Exercise 4.3
Textbook from page 83 – 92:
Part 8 Addition in C# .NET
Part 9 Subtraction in C# .NET
Part 10 Multiplication and Division in C#.NET
• Summary
Variables in details
Number – byte, int and long
Decimal – float and double
Character
Boolean
String
Implicit and explicit conversion
Numbers, Decimals and their operations (=, +, -, *, / and others)
Strings and operations (assigning value, concatenation)