Module 37565
Module 37565
Data Types
I. Preparation
At the end of this module students will:
embed comments in the code;
examine the different data types;
construct variables and constant;
implement server-side controls: Labe, TextBox and Button
implement the .toString() method
II. Presentation
Comment code is important part of any kind of programming language. Comment code
is only visible at the part of programming side while run the program the comment code
invisible at output side.
While complex programming have more code at programmer side and many
programmer can work on one project so there is a need to define the code logic with header
comment code so other programmer can easily understood the code.
In single line comments, the syntax is to use double forward slash “//”.
Multi Lines Comments in ASP.Net
For multi lines comment write code between “/* ” and “*/”.
Example
Variable in C#
Variable is a nothing but a memory location for storing value or variable is a
storage area.
Importance of Variable
A variable is a name given to a value.
A variable can hold value of specific data types like : int, string, float etc.
A variable can be initialized after declaration or initialized and declared at the same
time.
The value of variable can be changed at any time in a scope.
Can not declared multiple variable with same name within a scope.
Variable can be declared and initialized later in program or it can be initialized and the
declared at the same time.
Declaring Constants
A constant contains information that does not change during the course of program
execution.
Syntax:
const <data type> VARIABLENAME = value;
Example:
const int COPYRIGHT = 2020;
C# Variable declaration
Syntax:
<data_type> <variable_name>
Example:
int myno;
string name;
Here, int and string are data type and myno and name are variable name
Syntax:
<data_type> <variable_name>
<variable_name> = <value>
Example :
int myno;
myno = 10;
string name;
name = “CTU”;
In the above example, in first line, is the declaration of variable myno with int data type.
In the second line, value 10 is assign to variable myno. The next line, the name variable is declared
with string data type and the value ‘CTU’ is assigned to name variable.
Example
Integer Type Variable in C#.Net
An integer include whole numbers without decimal point.
Example:
Open Visual Studio –> New Website –> Add New Web form in Solution Explorer.
Go to .cs page declare variable with int data types as shown in the figure below.
In this code, myno variable is declared with int data type but not initialize so the c# compiler
shows warning message like The variable ‘myno’ is declared but never used.
In this example, the variable myno is declared, has initial value of 10 and display the value of
myno using Response.Write(myno), the compiler shows no warning message.
Take note: In c#.net textbox control values are always string value, use string variable for hold
textbox value. If we want to get value from textbox control then we must use string type variable for
store textbox value.
<data_type> <Variable_name>
Example:
Open Visual Studio –> New Website –> Add New Web form in Solution Explorer.
Go to .cs page declare variable with string data types show like below screen.
Here, strval variable is declared with string data types and after in second line “Me Academy”
is initialized to variable strval. Above variable declaration first line shows green underling warning
message, because the strval variable is declared and initialized but it is never used in the program.
String Implementation
Design web page with one textbox, a button and a label control. Store textbox value in string
variable and then assign string variable value to label control for display output on web form.
Below is the sample code for above example. In the first line, the value of textbox is assign to
string variable strval and then value of strval variable is assign label control.
float and double data type
Float and double type of variable store point value something like : 8.10, 123.56.
Declaration of float and double type of variable are same as integer type of variable.
The difference between float and double is the size of the numbers that they can hold.
The float variable can have up to 7 digit numbers. Float is a 32 bit number.
The double variable can have up to 16 digit numbers. Double is a 64 bit number.
Example :
float fvar;
fvar = 18.5F;
The capital letter ‘F’ on the end means float variable. If you declare float without ‘F’ letter
on end the c# compiler shows error message.
double dvar;
dvar = 18.5;
Here, double variable value no need to write ‘F’ at end of the value.
Example
Open visual studio –> Create new website –> Add new web form –> Design form. Design
web form with two button control one for get float value and second for get double value.
Here, declare float and double variable with some values and get that variable value on web
form while fire the button click event in c#.net like below screen.
In the above code example, the fval = 123456.7869F is declared float and display value in
label control lblfloat when button btnfloat is clicked. The result Float = 123456.8 because float
variable can hold up to 7 digit numbers. The last 8th digit number is 8 so the 7 digit converted to 8
digit then the result is 123456.8.
Example :
III. Practice
Direction: Identify if the give variable is valid or invalid. If valid, determine the
possible data type
IV. Performance
Direction: Identify the possible data type and make a variable name for each of the data
items below. Write your answer in a space provided