![]() |
ASP.Net Tutorial #6
Welcome to the sixth C# tutorial (still haven't found a better way to start these tutorials). Now we are getting to the nitty gritty and to the heart of C# with the introduction of classes. Now it has to be said that up till now I have not been a big fan of classes but they are growing on me and they are implemented quite nicely in C# (which the language I am using here to code ASP.NET). So what is the big mystery? What ARE classes? Well, I've already given you a big clue in the previous tutorial. A Class is a mixture of structures (struct) and functions in one construct. Here is what a simple class looks like with its calling program:-
<%@ Import namespace="System" %>
<html>
<head>
<title>Classes</title>
</head>
<body>
<H2>Classes</H2>
</body>
</html>
<script language="c#" runat="server">
public class HelloWorld
{
public int i = 1;
public string strMsg;
public void ShowHello()
{
strMsg = "Hello World with Classes!" + "<BR>" ;
HttpContext.Current.Response.Write(strMsg);
}
}
private void Page_Load(object sender, System.EventArgs e)
{
HelloWorld hw = new HelloWorld();
hw.ShowHello();
Response.Write(hw.i + "<BR>");
hw.i=2;
Response.Write(hw.i + "<BR>");
}
</script>
What I have done is create my own class which I can call upon from the Page_Load program. I can create an instance of it and call functions housed within it. I can also modify variables contained within it much like in a struct. All very clever.
So when should you use classes? Well, you could program in C# and NEVER use them (much hissing from the auditorium and shouts of "Heresey!") at all. But you would be missing out on a whole lot of stuff. I'm not going into classes any more deeply at this stage but there are some things you ought to know about classes.
It is my aim in these tutorials to teach you enough to be able to use databases and Forms and you don't necessarily need all the extra stuff to do it but I may return this subject in later tutorials. Here is ah enhancement to the above program:-
using System;<%@ Import namespace="System" %>
<html>
<head>
<title>Classes</title>
</head>
<body>
<H2>Classes</H2>
</body>
</html>
<script language="c#" runat="server">
public class HelloWorld
{
public int i = 1;
public string strMsg;
public void ShowHello()
{
strMsg = "Hello World with Classes!" + "<BR>" ;
HttpContext.Current.Response.Write(strMsg);
}
}
public class ShowTime
{
public void ShowCurrentTime()
{
HttpContext.Current.Response.Write
("The current time is: " + DateTime.Now);
}
}
private void Page_Load(object sender, System.EventArgs e)
{
HelloWorld hw = new HelloWorld();
hw.ShowHello();
Response.Write(hw.i + "<BR>");
hw.i=2;
Response.Write(hw.i + "<BR>");
ShowTime st = new ShowTime();
st.ShowCurrentTime();
}
</script>
This enables you to call a class to get the current time. I was alluding in previous tutorials to how classes can contain both data structures and functions so here is a sample:-
<%@ Import namespace="System" %>
<html>
<head>
<title>Classes 2</title>
</head>
<body>
<H2>Classes 2</H2>
</body>
</html>
<script language="c#" runat="server">
public class clsPhone
{
public string strName;
public string strNumber;
public void AddPhone()
{
strName="Nick";
strNumber="01234567890";
}
}
private void Page_Load(object sender, System.EventArgs e)
{
clsPhone pb = new clsPhone();
pb.AddPhone();
Response.Write(pb.strName + "<BR>");
Response.Write(pb.strNumber + "<BR>");
}
</script>
Here we have both a structure than can be accessed as if it was a structure and functions that can update them. I hope that all this will become clearer as we move further into these tutorials. So that's it with classes.
One other thing to aquaint you with (yes databases is next!) is error checking. This is done using try, catch, finally which has been part of C++ and Java for a while but has made it into C# (and Visual Basic.Net as well). Here's some code:-
<%@ Import namespace="System" %>
<%@ Import namespace="System.Runtime.InteropServices" %>
<html>
<head>
<title>Function</title>
</head>
<body>
<H2>Function</H2>
</body>
</html>
<script language="c#" runat="server">
private void Page_Load(object sender, System.EventArgs e)
{
try
{
Response.Write("Try" + "<BR>");
int a=0;
int b=100/a;
}
catch (Exception ex)
{
Response.Write("Catch" + "<BR>");
Response.Write(ex.Message + "<BR>");
}
finally
{
Response.Write("Finally" + "<BR>");
}
}
</script>
It was quite difficult to find a simple bit of code to trip an exception (error to you) but here is a divide by zero error sample. The code we want to use goes inside the try section which would usually be some kind of open command which may trip an error if a file doesn't exist for example. If an error is generated then the catch part comes into play and generates an error message. The finally bit ALWAYS runs and would normally contain the file close command so the file would be closed no matter what. We will need this in the next tutorial (which will be databases...honest). See you then...