![]() |
ASP.Net Tutorial #2
Welcome to the second ASP.Net tutorial. It is my intention in these tutorials to offer example programs that demonstrate various aspects of ASP.Net but using as little code as possible. Programming by its very nature can be verbose and can sometimes hide what are very simple concepts at heart.
You will need to be running Windows XP with versiion 1.1 of the .Net framework installed (its on the WinXP CD) with Service Pack 2 also installed (you shouldn't be running XP without it!). You will also need a web server running as detailed in the first tutorial. Your only other application will be notepad and that's it! No need to buy expensive Integrated Development Environments (IDEs) although it will help. On with our next example:-
<%@ Import namespace="System" %>
<html>
<head>
<title>Variables 2</title>
</head>
<body>
<H2>Variables 2</H2>
</body>
</html>
<script language="c#" runat="server">
private void Page_Load(object sender, System.EventArgs e)
{
int i;
char k;
string hello;
double d;
i=1;
k='a';
hello="hello";
d=1.234567890;
Response.Write(i + "<BR>");
Response.Write(k + "<BR>");
Response.Write(hello + "<BR>");
Response.Write(d + "<BR>");
i=2;
k='b';
hello="bye";
d=3.234567890;
Response.Write(i + "<BR>");
Response.Write(k + "<BR>");
Response.Write(d + "<BR>");
Response.Write(hello + "<BR>");
}
</script>
Don't worry that it looks a bit long - it is really just repeating the same thing over and over again. Here we are interested in what are called variables. They are variables because what they contain can vary. Variables come in many guises (there are more but these four will do for the moment). First there is an integer which is shortened to int and is the first statement after the HTML. An integer can contain numbers without a decimal point and in this case we have called our variable with the single letter i so
int i;
(note the terminating semicolon) means that we have created a variable that will contain a number without a decimal point but as yet it doesn't contain anything. Now it is important to ensure that ALL variables are initialised with a value of some kind as it might contain garbage (this isn't quite such a problem with modern compilers but it is good practise to assign a value anyway). So if you look further down the program, we see this:-
i=1;
which assigns a value of 1 to the variable. Further down the program we find:-
Response.Write(i + "<BR>");
which will write the value of i (which is currently set to 1) and a newline which is in HTML to the browser window just to prove that it is doing what we expect. The next block has the integer value of i changed to 2 - this isn't being added to the variable, it is replacing what was there before and the next block displays the new value.
Going back to the top of the program, there are other variables that are being created, assigned, displayed, changed and displayed again. These are:-
char k;
This holds a single letter of the alphabet which can letters A-Z or a-z or any of the other characters available on the keyboard. This could confusingly include numbers 0-9 but it would store it as a letter NOT a number so we couldn't use this variable to perform mathematics on (such as adding to it). Because it is a character, the assignment has to be surrounded by single quotes as in:-
k='a';
Another type of variable is a string:-
string hello;
It should be noted that variable names can be single letter or use more that one as shown here with the varibale name called hello. It is usually wise to create meaningful variable names so you use a name that gives you an idea what it can contain. This is a string of characters so it is assigned as follows:-
hello="hello";
The variable name is used in exactly the same way in the writeline statements as all the others. Finally there is the double which is a number but it can contain a decimal point as shown. Compile the program and watch how the variable contents change. This is a completely new program so create a new file (vars.cs for example) and compile it with the new name which will create a new executable program with this name.
Now here is a little homework for you. Try changing the contents of a variable by adding something to it. For example, to add 1 to the variable i we would use the statement:-
i=i+1;
and use the response.write statement to verify that it does actually change.
There is another way of writing variable creation and assigment. Heres another new program (vars2.aspx perhaps) which illustrates this:-
<%@ Import namespace="System" %>
<html>
<head>
<title>Variable 2</title>
</head>
<body>
<H2>Variables 2</H2>
</body>
</html>
<script language="c#" runat="server">
private void Page_Load(object sender, System.EventArgs e)
{
int i = 1;
char k = 'a';
string hello = "hello";
double d = 1.234567890;
Response.Write(i + "<BR>");
Response.Write(k + "<BR>");
Response.Write(hello + "<BR>");
Response.Write(d + "<BR>");
i=2;
k='b';
hello="bye";
d = 2.234567890;
Response.Write(i + "<BR>");
Response.Write(k + "<BR>");
Response.Write(hello + "<BR>");
Response.Write(d + "<BR>");
}
</script>
It shows that a variable can be created and assigned a value in one statement as in:-
int i = 1;
This takes up less room and is a little more pleasing to the eye. Once again, the second block of assignments is the same as before because you cannot create variables with the same name in the same program. Something else to note when you run these is that the HTML heading text is printed second even though it is at the top of the file. This is because the Page_Load function is run first before the HTML which can be useful. In this case, it would be just a matter of using a response.write statement to write the HTML at the top of the script. You might like to have a go at trying to implement this. I think that should give you something to think about ready for when I see you again for the next tutorial.