0% found this document useful (0 votes)
32 views11 pages

Module 37565

The document discusses different data types in C# including integer, string, float, and double. It explains how to declare and initialize variables of each data type, and provides examples of implementing variables in ASP.NET code.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views11 pages

Module 37565

The document discusses different data types in C# including integer, string, float, and double. It explains how to declare and initialize variables of each data type, and provides examples of implementing variables in ASP.NET code.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Module 3

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

How to comment C# code in asp.net

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.

Two type of comments


 Single Line Comments
 Multi Lines Comments
Single Line Comments ASP.Net

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.

Each variable in C# must be declared with specific data types, which


determine the size and type of value variable can store.Variable is a storage area
which store the values, values may be numbers, strings , floating values or arrays.
Data type should be a int, string, char, float.

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.

Working with Data Types


In C#, the basic data types: int, float, char, string and bool.

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

Variable Initialized after Declaration

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.

Variable Initialized with Declaration


In C#, variable declaration and variable initialization is supported at the same time.

Example
Integer Type Variable in C#.Net
An integer include whole numbers without decimal point.

Example:

int myno; //here int = datatype and myno = variable name

myno=10; //here we assign value 10 to myno variable.

For example, in c# .net to understand integer type variable:

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.

Initialize variable after declaration


In this example, variable myno with int data type and has initialize value of 10, but still the
variable is not been used in the program so the c# compiler shows warning message like: The variable
‘myno’ is assigned but its value is 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.

String Type Variable in C#.Net


String variable always stores text value in c#. The string data types store the text value, a
number of character. String variable store value always with in double quote(“ ”).

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.

C# string variable declaration


Syntax:

<data_type> <Variable_name>

Example:

string strval; //here string = datatype and strval = variable name

strval=10; //here we assign value 10 to strval string variable.


For example, in c# .net to understand string type variable:

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.

Code for above example:

protected void btnfloat_Click(object sender, EventArgs e)


{
float fval = 123456.7869F;
lblfloat.Text = "Float = "+ fval.ToString();
}
protected void btndouble_Click(object sender, EventArgs e)
{
double fval = 123456.7869;
lbldouble.Text ="Double = " + fval.ToString();
}

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 :

float fval1 = 123456.7869F //then the result = 123456.8


float fval2 = 123456.743F //then the result = 123456.7
Same thing of the fval = 123456.7869 is declared double. The result is Double =
123456.7869, because double variable can have up to 16 digit numbers.

The .toString method()


In C#, the ToString() method is used to convert an object to its string representation.
It is commonly used to convert values of primitive data types such as int, float, double, and
bool to strings.

III. Practice

Direction: Identify if the give variable is valid or invalid. If valid, determine the
possible data type

Variable Name Valid/Invalid Data Type

1. Gross_pay ___________, ____________


2. _ema1L ___________, ____________
3. Fam-name ___________, ____________

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

Data Item Data Type Variable Name Declaration


1. Age of a person ___________, ______________ _______________
2. Population of a city ___________, ______________ _______________
3. Price of an item ___________, ______________ _______________

You might also like