ASP.NET Basics (part 1): Nothing But .Net - Naming Names (Page 6 of 8 )
Let's move on to variables. Variables are the bread and butter of every programming language...and ASP.NET has them too. A variable can be thought of as a programming construct used to store both numeric and non-numeric data; this data can then be used in different places in your C# code.
The .NET Framework and all its programming languages - C#, VB.NET, Jscript.NET et al - support a number of different variable types: integers, floating point numbers, strings and arrays. Unlike some scripting languages, which can automatically determine variable type based on the data it holds, .NET languages require you to explicitly define the type of each variable before using it.
Every variable must have a name - this name is used to refer to the variable, to perform operations on it, and to retrieve the data it contains. In C#, a variable name is preceded by a keyword indicating the variable type, and must begin with a letter, optionally followed by more letters and numbers. Variable names are case-sensitive, and reserved keywords cannot be used as variable names. For example, "popeye", "one_two_three" and "bigPirateShip" are all valid variable names, while "byte" and "123" are invalid variable names.
The following example demonstrates how variables can be used in a ASP.NET
document:
<script language="c#" runat="server">
void Page_Load()
{
string name; // define a variable as a string type
name = "Neo"; // put some initial value into it
whoami.Text = "I am " + name + ". Welcome to my world.";
}
</script>
<html>
<head><title>Who am I?</title></head>
<body>
<asp:label id="whoami" runat="server" />
</body>
</html>
Here's the output:

As you can see, I have first defined a variable named "name", set things up so that it will hold string data, and assigned a value ("Neo") to it. In my HTML code, I have defined a "label" server control called "whoami". In the
Page_Load() function, I have used basic string concatenation to incorporate the value of the "name" variable into the "Text" property of the "whoami" label.
If you alter the value assigned to the "name" variable, the output of the page generated will change to reflect the new name. Try it and see for yourself!
You can also define a variable without assigning a value to it, or assign a value to it at a later stage - for example, the following code snippets are equivalent.
string name
; // define the variable
name = "The One"; // and initialize it
string name
="The One"; // define and initialize a variable simultaneously
C# is a strongly typed language - in simple English, this means that each variable can only store a particular type of data. For example, I have defined the variable as a "string" datatype. I cannot store a number in this variable; if I try, as in the example below, I'll see an ugly error
message:
// define a variable as string and initialize it to a number
string num = 1;
Define the number as a string, though, and it works out fine:
// define a variable as string and
// initialize it to a number stored as a string value
string num = "1";
Of course, the drawback of the above solution is that the "name" variable does not store the value "1" as a number and hence cannot be used for operations like addition, subtraction or multiplication.
Next: Linking Out >>
More ASP.NET Code Articles
More By Harish Kamath (c) Melonfire