Data Type and Variable Introduction Data Type In

Data Type and Variable

Introduction: Data Type In real world, we can see that there at least three types of objects that we commonly see, that are: ◦ Solid ◦ Liquid ◦ Gas If you want to sum the objects, it is common for us to measure them in the same types, even to the same unit. For example, you “cannot” add the following objects:

Introduction: Data Type q. But you can add the following objects: q. Lesson learnt: to add two objects you have to ensure that both objects are of the same types. q. If the objects types are not the same, try to change (convert) one object type to the other type (for example, convert the ice (solid) to water (liquid)

Introduction: Data Type q. C# also has the objects types. q. Three basic/primitive “objects types” (or more commonly known as “data types”) that we will learn are: qstring qint qdouble text whole/integer number (bil. Bulat) floating point number (bil. Desimal) q. Example data for each type: qstring : “my name is Richo”, “My favorite color is Blue”, etc. qint : 20, 900, 75000, etc. qdouble : 3. 75, 79. 99, 15000. 75, etc. q Note: the decimal separator is a DOT, and there is no thousands separator.

Introduction: variable q. As shown on the previous slide, you need a storage or container to store the object. q. Some examples of containers in real world: q. Glass, Jug, Bottle, etc q. Basket, Carton Box, etc q. Tank : to store liquid object : to store solid object : to store gas

Introduction: variable q. Each container can be used perfectly for a certain object type only. q. For example: q. You cannot store liquid in a basket, q. You cannot store solid object in a oxygen tank q. You cannot store the pure oxygen into a glass, qetc.

Introduction: variable q. C# also use storage/container to store the data/value. q. In C#, the storage/container is called: variable q. The storage type must match to the data type to be stored. q. For example: q. Text must be stored in a string variable q. A whole number must be stored in an int variable q. A floating point number must be stored in a double variable

Creating variable in C# q. Example: q. Creating string variable named: fav. Color string fav. Color; q. Creating int variable named: age int age; q. Creating double variable named: discount double discount; q. Note: q. Each variable needs to be created ONCE only q. Two different variables cannot have the same name

Fill the variable with data Some possible ways to fill the variables. Each example can be used for ALL variable types. Create and fill separately string fav. Color; fav. Color = “Red”; Create and fill at the same time: int age = 20; Create and fill variable from calculation: double discount = 0. 1; double amount. Disc; amount. Disc = discount * 1000000;

The size of the variable How small and big the data that can we store in a variable? • string: can store until 2 billion characters • int: -2, 147, 483, 648 to 2, 147, 483, 647 • double : ± 5. 0 × 10− 324 to ± 1. 7 × 10308 So if you accidentally store number: 5 billion (5, 000, 000) to an int variable, then don’t be surprise if you don’t get the correct number stored in your variable.

Add the content of the variable All variables that involved in the Add process must have the same data type. Example: string name; string first. Name = “Richo”; string last. Name = “Hardana”; name = first. Name + “ ” + last. Name; You will get string “Richo Hardana” inside the string variable: name

All variables that involved in the Add process must have the same data type. Example: int first. Num = 10, second. Num = 20; int total; total = first. Num + second. Num; You will get 30 inside the integer variable: total Note: you can make your own example for variable with type: double

Can we mixed the data type? Example: string str. First. Num = “ 10”; int second. Num = 20; int total; total = str. First. Num + second. Num; You will get ERROR when VS tries to run this statement: total = str. First. Num + second. Num; Why?

Can we mixed the data type? The answer is: NO If you have different data types in an expression, then try to convert them to become the same. C# will only execute the expression if ALL the data in the variables have the same data type.

◦To int: use method int. Parse(string) Example: int age = int. Parse(“ 20”); int mark = int. Parse(text. Box. Mark. Text); ◦To double: use method double. Parse(string) Example: double disc = double. Parse(“ 0. 25”); double price = double. Parse(text. Box. Price. Text);

Converting data type: from int ◦To double: will be performed automatically by VS by adding the dot zero (. 0) behind the number Example: double disc = 5; // stored as 5. 0 int mark = 92; double mark. Std = mark; // stored as 92. 0 ◦To string: use method. To. String() Example: int age = 5; string str. Age = age. To. String(); // str. Age=“ 5”

Converting data type: from double ◦To int: use typecasting (write the destination data type inside a bracket) Example: double total = 1000. 9; int total 2 = (int) total; //stored as 1000 ◦To string: use method. To. String() Example: double disc = 0. 2; string Str. Disc = disc. To. String(); //str. Disc=“ 0. 2”

Arithmetic Operation Operator “+” can be used for all primitive data types. Be aware that: ◦ “ 1” + “ 2” + “ 3” will give you “ 123”, but ◦ 1 + 2 + 3 will give you 6 Other operators: ◦ minus (“-”), ◦ multiply(“*”), ◦ divide(“/”) can only be used for int and double only.

Naming Rules NO SPACE Cannot start with number ONLY use the following character ◦ Letter (a-z, A-Z) ◦ Number (0 -9), or ◦ underscore ( _ ) Cannot use the same name as the one that is already used by C# (for example: double, string, Form, etc)

Which Name is Incorrect? Why? number. Of. Students double _start. Engine %tage. Of. Smart. Student 2 birds. On. The. Line my. Address. In. www top ten students AUDTORUPIAH payment. Must. Be. In$ studentmarksin 4 tests long-Line

Notes about Naming UPPERCASE is different from lowercase. Example: student. Age and Student. Age are different. Create name that represent its purpose. Example: if you want to create an object to store information about the current year, then a name such as “current. Year” is better than “c” for example. Variable Name: except for the first word, start each word with uppercase then lowercase. Example: expire. Date, human. Age, number. Of. Books, etc. Controls Name: start the name with the type of the controls. Example: button. Exit, text. Box. Address, etc.

Exercise 1 – Simple Calculator

Exercise 2 – Simple Calculator Ver 2. 0
- Slides: 23