Home

Introduction to Values in a Webpage

Fundamentals of Variables

Introduction to Variables

A variable is a value or data that must be stored in the computer memory and accessed when necessary. Before using or accessing that variable, you must declare it. This is done by providing at least three pieces of information: a name for the variable, its type of value, and an initial value to store in the computer memory.

The Name of a Variable

A variable must have a name. There are keywords you must not use because the language itself uses them. These keywords are:

abstract continue finally interface out (generic) short typeof
as decimal fixed internal out (methods) sizeof uint
base default float is override stackalloc ulong
bool delegate for lock params static unchecked
break do foreach long private string unsafe
byte double goto namespace protected struct ushort
case else if new (generic) public switch using
catch enum implicit new (LINQ) readonly this virtual
char event in (foreach) new (variable) ref throw void
checked explicit in (generic) null return true volatile
class extern int object sbyte try while
const false   operator sealed    

There are other names that are not considered C# keywords but should be avoided because they may cause a conflict in your code. They are referred to as contextual keywords and they are:

add descending global let remove var
alias dynamic group orderby select where (generic)
ascending from into partial (method) set where (query)
async get join partial (type) value yield
await          

As mentioned already, there are rules you must observe to name anything in your program:

Besides these rules, you can also create your own rules but that follow the above restrictions. For example:

C# is case-sensitive. This means that the names Case, case, and CASE are completely different

The Type of a Variable

The amount of computer memory necessary to store the value of a variable is called a data type.

Declaring a Variable

To declare a variable, you have two options:

Initializing a Variable

Initializing a variable consists of storing an initial value in its reserved area. To assign a value to a variable:

Introduction to Using a Variable

Introduction to Strings

A string is an empty space, a character, a word, or a group of words. The value of a string starts with a double quote and ends with a double-quote. To declare a variable for a string, use either the var or the string keywords followed by a name for the variable. When declaring a string variable, you can initialize it with an empty space, a character, a symbol, a word, or a group of words. Here are two examples:

<%@ Page Language="C#" %>

<!DOCTYPE html>

<html>
<head>
<title>Employee Record</title>

</head>
<body>

<h2>Employee Record</h2>

<%
    string firstName = "Philippe";
    string lastName = "Fields";
%>

</body>
</html>

Responding to the Value of a Variable

To display the value of a string variable, you can simply put it in the parentheses of Response.Write(). Here are examples:

<%@ Page Language="C#" %>

<!DOCTYPE html>

<html>
<head>
<title>Employee Record</title>

</head>
<body>

<h2>Employee Record</h2>

<%
    string firstName = "Philippe";
    string lastName = "Fields";

    Response.Write("First Name: ");
    Response.Write(firstName);
    Response.Write("<br>");
    Response.Write("Last Name: ");
    Response.Write(lastName);
%>

</body>
</html>

This would produce:

Fundamentals of Web Forms

Primary Topics on Variables

Declaring Many Variables

If your code needs many variables, you can declare each on its own line. Here are examples:

<%
    string firstName;
    string lastName;
    string fullName;
%>

If many variable use the same data type, you can declare them them as a group. Write the data type followed by each of the names of the variables separated by commas. Here is an example:

<%
    string firstName, lastName, fullName;
%>

The variables can also be initialized. In this case, a variable must have its own initialization. You don't have to initialize each variable if it is not required. Here are examples:

<%@ Page Language="C#" %>

<!DOCTYPE html>
<html>
<head>
<title>Employee Record</title>
</head>
<body>
<h3>Employee Record</h3>

<%
    string firstName = "John", lastName = "Doe", fullName;

    Response.Write("First Name: ");
    Response.Write(firstName);
    Response.Write("<br>");
    Response.Write("Last Name: ");
    Response.Write(lastName);
%>

</body>
</html>

Assigning a Value to a Variable

After declaring and initializing a variable, if you want to change its value at any time, type its name, followed by =, and followed by the desired value. This is referred to as assigning a value to the variable. Here are examples:

<%@ Page Language="C#" %>

<!DOCTYPE html>
<html>
<head>
<title>Payroll Preparation</title>

</head>
<body>
<h3>Payroll Preparation</h3>

<%
    string firstName = "John", lastName = "Doe";

    Response.Write("First Name: ");
    Response.Write(firstName);
    Response.Write("<br>");
    Response.Write("Last Name: ");
    Response.Write(lastName);

    firstName = "David";
    lastName = "Absalom";

    Response.Write("<br><br>First Name: ");
    Response.Write(firstName);
    Response.Write("<br>");
    Response.Write("Last Name: ");
    Response.Write(lastName);

%>

</body>
</html>

This would produce:

String Addition

Characters

A character is anything that can be displayed as a symbol. To declare a variable for a character, you can use the var or the char keywords and initialize the variable with a character in single-quotes.

Introduction to Number-Based Variables

Introduction to Integers

A natural number is a value made of one or a combination of digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9. C# supports various types of numbers. To declare a variable that will hold natural numbers, use a data type named int. Here is an example:

<%
    int yearlySalary = 48600;
%>

Value Types

When you declare a variable using a numeric type such as those we will see in this lesson, the variable is stored in a section of memory named the stack. The values of the variables in this lesson are referred to value variables or value types.

Signed and Unsigned Integers

A number is negative if it is lower than 0. Such a number must be preceded by -. If a number is higher than 0, it is said to be positive. Such a number can be preceded by + or no sign. The ability for a number to hold a sign makes it possible to say that such a number is signed.

Any number that uses neither - or + is said to be positive and, because it doesn't use a sign, it is said to be unsigned.

Parsing the Value of a Control

The value of a text-based control is primarily considered a string. If the control is supposed to provide a number, before using its value, you must first convert it to the appropriate type. If the value is an integer, to convert it, use int.Parse(). In the parentheses, write the identifier of the control followed by .Text. You can then store this expression in a variable.

Converting a Value to a String

To convert the value of a variable to a string, type the name of the variable followed by ToString(). Here are examples:

<%@ Page Language="C#" %>

<!DOCTYPE html>
<html>
<head>
<title>Algebra</title>
</head>
<body>
<h3>Algebra</h3>

<%
    int number1 = 2739;
    int number2 = 534;
    
    int total = number1 + number2;

    Response.Write("Total = ");
    Response.Write(total.ToString());
%>
</body>
</html>

This would produce:

Converting a Value to a String

Integers

Bytes

A byte is an unsigned number whose value can range from 0 to 255. To declare a variable that would hold a small natural number, use the byte data type. To convert the value of a control to a byte, use byte.Parse(). In the parentheses, type the name of the variable followed by .Text. To convert a byte value to a string, type the name its variable followed by .ToString().

Signed Bytes

A byte number is referred to as signed if it can hold a negative or a positive value that ranges from -128 to 127. To declare a variable for that kind of value, use the sbyte data type. To convert the value of a control to an sbyte, use sbyte.Parse(). In the parentheses, type the name of the variable followed by .Text. To convert an sbyte value to a string, type the name its variable followed by .ToString().

Short Integers

A short integer is a natural number between -32768 and 32767. To declare a variable for such a value, use short keyword. To convert the value of a control to a short integer, use short.Parse(). In the parentheses, type the name of the variable followed by .Text. To convert a short value to a string, type the name of its variable followed by .ToString().

Unsigned Short Integers

An unsigned short integer is a number between 0 to 65535. To declare a variable for such a number, use either the var keyword or the ushort data type.

Signed Integers

A signed integer is a number between 2,147,483,648 and 2,147,484,647 negative or positive. To declarea a variable that can hold small to quite large numbers, use the int keyword (we saw some examples earlier).

Unsigned Integers

An unsigned integer is a positive number between 0 and 4,294,967,295. To declare a variable for such a number, use the uint data type.

Long Integers

A long integer is a number between -9,223,372,036,854,775,808 and 9,223,372,036,854,775,807. To declare a variable for such a number, use the long keyword. To convert the value of a control to a long integer, use long.Parse(). In the parentheses, type the name of the variable followed by .Text. To convert a long integer value to a string, type the name of its variable followed by .ToString().

Unsigned Long Integers

An unsigned long integer is a positive number between 0 and 18,446,744,073,709,551,615. To declare a variable that can hold such a number, use a data type named ulong.

Topics on Integers

When assigning a value to an integer-based variable, make sure you don't provide a value that is higher than the type allows. Otherwise you would receive an error.

Unless you have a good reason, avoid using unsigned data types because many languages don't explicitly support them (most C-based languages (C, C++, and C#) support them, so do F# and PHP; but many languages don't). If you happen to convert your code to another language, unsigned integers may present some challenges (not that the conversions are impossible but the exercise may make waste more time than you need).

Instead of a decimal number, you can initialize an integral variable with a hexadecimal value.

Floating-Point Numbers

Double-Precision Numbers

A decimal number is made of one or two sections. It can contain only digits. If it contains a fraction, its two parts must be separated by a period (in US English). To declare a variable for a decimal number, use a data type named double. Here is an example:

double number = 62834.9023;

To convert the value of a variable to a double-precision number, use double.Parse(). In the parentheses, write the value.

To convert the value of a variable to a string, type the name of the variable followed by ToString().

Number Formatting

ToString() provides the default way to convert and display a number. There are more detailed formats available. To use them, the parentheses of ToString() must contain a string as a letter in double-quotes. The allowed letters and their meanings are:

Character Description
c C Currency values
d D Decimal numbers
e E Scientific numeric display such as 1.45e5
f F Fixed decimal numbers
d D General and most common type of numbers
n N Natural numbers
r R Roundtrip formatting
s S Hexadecimal formatting
p P Percentages

Floating-Point Numbers

The most basic floating-point number is a value between �1.5 x� 10−45 and �3.4 x� 1038 with a precision of 7 digits. To declare a variable for suh a number, use a data type named float. Here is an example:

float numberOfBathrooms;

The floating-point number with double-precision is an alternative. We already saw that a variable for such a number could be declared using the double keyword. A variable declared as double can store very large numbers ranging from �5.0 x 10−324 to �1.7 x �10308 with a precision of 15 or 16 digits. Because both the float and the double data types use the same types of numbers, when assigning the value of a float variable, add an f or an F suffix to the value. Here is an example:

float distance = 248.38F;

On the other hand, if you want a value to be treated with double-precision, add a d or a D suffix to it.

Decimal

The decimal data type can be used to declare a variable that would hold significantly large values that can range from �1.0 x� 10−28 to �7.9 x� 1028 with a precision of 28 to 29 digits. To indicate that the variable holds a decimal value, when initializing it, add an m or an M suffix to its value.

Topics on Declaring and Using Variables

Constants

A constant is a value that never changes. To create a constant, use the const keyword followed by the desired data type, followed by a name for the variable. Then assign the desired value. Here is an example:

<%
    const double PI = 3.14159265359;
%>

Once a constant has been created and it has been appropriately initialized, you can use its name where the desired constant would be used. Here is an example:

<%@ Page Language="C#" %>

<!DOCTYPE html>
<html>
<head>
<title>Geometry - Circle</title>
</head>
<body>
<h3>Geometry - Circle</h3>

<%
    const double PI = 3.14159265359;
    double radius = 148.27;

    double diameter = radius * 2.00;
    double circumference = diameter * PI;
    double area = radius * radius  * PI;

    Response.Write("<table border=5><tr><td>Radius:</td><td>");
    Response.Write(radius.ToString());
    Response.Write("</td></tr><tr><td>Diameter:</td><td>");
    Response.Write(diameter.ToString());
    Response.Write("</td></tr><tr><td>Circumference:</td><td>");
    Response.Write(circumference.ToString());
    Response.Write("</td></tr><tr><td>Area:</td><td>");
    Response.Write(area.ToString());
    Response.Write("</td></tr></table>");
%>
</body>
</html>

This would produce:

Constants

Declaring a Variable for Nullity

A value is referred to as null when it cannot be clearly determined. A null value is not 0 because 0 is a an actual number. In C#, normally, not all variables can hold null. When declaring a variable, to indicate that it can hold either an actual value or it can be null, the formula to follow is:

data-type? variable-name;

This formula means, put a question mark between the data type and the name of the variable. The question can be stand in the middle or be attached to either the data type or the variable. Here are examples:

<%@ Page Language="C#" %>

<!DOCTYPE html>
<html>
<head>
<title>Geometry - Circle</title>
</head>
<body>
<h3>Geometry - Circle</h3>

<%
    const double PI = 3.14159265359;
    double? radius = 148.27;
    double ?diameter = 0.00;
    double ? circumference = 0.0;
    double? area = 0.00;


    diameter = radius * 2.00;
    circumference = diameter * PI;
    area = radius * radius  * PI;

    Response.Write("<table border=5><tr><td>Radius:</td><td>");
    Response.Write(radius.ToString());
    Response.Write("</td></tr><tr><td>Diameter:</td><td>");
    Response.Write(diameter.ToString());
    Response.Write("</td></tr><tr><td>Circumference:</td><td>");
    Response.Write(circumference.ToString());
    Response.Write("</td></tr><tr><td>Area:</td><td>");
    Response.Write(area.ToString());
    Response.Write("</td></tr></table>");
%>
</body>
</html>

The Nullity of a Variable

As mentioned already, when declaring a variable, you may indicate that it can hold null by adding a question mark to its data type. To let you initialize the variable, the C# language provides the null keyword. To initialize a variable with it, simply assign null to it. Here are examples:

<%@ Page Language="C#" %>

<!DOCTYPE html>
<html>
<head>
<title>Payroll Preparation</title>

</head>
<body>

<%
    string firstName = null;
    string lastName = null;
    string fullName = null;

    Response.Write("<h3>Payroll Preparation</h3>");

    firstName = "Mark";
    lastName = "Jameson";
    fullName = lastName + ", " + firstName;

    Response.Write("<br>First Name: " + firstName + "<br>");
    Response.Write("Last Name: " + lastName + "<br>");
    Response.Write("Full Name: " + fullName);
%>

</body>
</html>

This would produce:

NULL


Previous Copyright © 2008-2016, FunctionX Next