Article

Object Oriented C# for ASP.NET Developers

Page: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Next

Using the Tree Class

Okay, so now you have the blueprint of a tree. Big deal, right? Where things get interesting is when you use that blueprint to create and manipulate Tree objects in a .NET program such as an ASP.NET Web page. Create a new file in your text editor called PlantTrees.aspx and follow along as I talk you through writing such a page.

First, here's a look at the full code for the page:

1  <%@ Page Language="C#" %>    
2  <html>    
3  <head>    
4  <title>Planting Trees</title>    
5  <script runat="server">    
6    protected void Page_Load(Object Source, EventArgs E)    
7    {    
8      string msg = "Let's plant some trees!<br/>";    
9    
10     // Create a new Tree    
11     Tree tree1 = new Tree();    
12    
13     msg += "I've created a tree with a height of " +    
14       tree1.height + " metre(s).<br/>";    
15    
16     tree1.Grow();    
17    
18     msg += "After a bit of growth, it's now up to " +    
19       tree1.height + " metre(s) tall.<br/>";    
20    
21     Tree tree2 = new Tree();    
22     Tree tree3 = new Tree();    
23     tree2.Grow();    
24     tree3.Grow();    
25     tree2.Grow();    
26     tree3.Grow();    
27     tree2.Grow();    
28     msg += "Here are the final heights:<br/>";    
29     msg += " tree1: " + tree1.height + "m<br/>";    
30     msg += " tree2: " + tree2.height + "m<br/>";    
31     msg += " tree3: " + tree3.height + "m<br/>";    
32    
33     Output.Text = msg;    
34   }    
35 </script>    
36 </head>    
37 <body>    
38 <p><asp:label runat="server" id="Output" /></p>    
39 </body>    
40 </html>

If you look at the bottom of the code, you'll see the HTML section basically just contains a single <asp:label> tag. Our Page_Load function is where all the action will happen, and it will use that <asp:label> to display the results of our messing around.

So let's focus on the code within Page_Load:

8      string msg = "Let's plant some trees!<br/>";

Here we're creating a text string (string) called msg. We'll use it to store the message that we'll eventually tell the <asp:label> tag to display. To begin with, it contains a little introductory message, with a <br/> tag at the end to create a new line on the page.

10     // Create a new Tree    
11     Tree tree1 = new Tree();

As the comment on line 10 suggests, line 11 achieves the feat of creating a new Tree out of thin air. This is a really important line; so let me explain it in depth. The line begins by declaring the class (type) of object to be created (in this case, Tree). We then give a name to our new Tree (in this case, tree1). This is in fact identical to declaring a new variable by specifying the type of data it will contain followed by the name of the variable (e.g. string msg).

The rest of the line is where the real magic happens. The word new is a special C# keyword that triggers the instantiation of a new object. After new comes the name of the class to be instantiated, followed by a pair of parentheses (again, in more complex cases that we shall see later, these parentheses may not be empty).

In brief, this line says, "create a variable of type Tree called tree1, and assign it a value of a new Tree." So in fact this line isn't just creating a Tree, it's also creating a new variable to store it in. Don't worry if this distinction is a little hazy for you at this point; later examples will serve to clarify these concepts significantly.

Now that we've created a tree, let's do something with it:

13     msg += "I've created a tree with a height of " +    
14       tree1.height + " metre(s).<br/>";

This should not be too unfamiliar to you. The += near the start of the line tells C# to add the following string to the string already stored in the msg variable. In other words, msg += is shorthand for msg = msg +. So this two-line command is just adding another line of text to the message, except that part of the line of text takes its value from the height of the tree1 variable (tree1.height). If you simply typed height instead of tree1.height, C# would think you were referring either to a variable called height declared in this method, or a tag with ID height (in the PlantTrees.aspx page itself). Unable to find either of these, your Web server would print out an error message when you tried to view the page. In order to tell C# that you are referring to the height field of the Tree in tree1, you need to tack on tree1 followed by the dot operator (.).

The dot operator may be thought of sort of like the C# way of saying "belonging to" when you read the expression backwards. Thus, tree1.height should be read as "height belonging to tree1." Since Trees are created with a height of zero, lines 13 and 14 should print out "I've created a tree with a height of 0 metre(s)."

Calling (or triggering) methods belonging to an object is accomplished in a similar way:

16     tree1.Grow();

This line calls the Grow method belonging to the Tree in tree1, causing it to grow by a metre. Again, the set of parentheses indicate that it is a method we are referring to, not a field. So after this line if we print out the height of tree1 again...

18     msg += "After a bit of growth, it's now up to " +    
19       tree1.height + " metre(s) tall.<br/>";

This line will print out "After a bit of growth, it is now up to 1 metre(s) tall."

To show that each Tree has its own height value that is independent of those of the other Trees, we'll polish off this example by creating a couple more Trees and having them grow by different amounts:

21     Tree tree2 = new Tree();    
22     Tree tree3 = new Tree();    
23     tree2.Grow();    
24     tree3.Grow();    
25     tree2.Grow();    
26     tree3.Grow();    
27     tree2.Grow();    
28     msg += "Here are the final heights:<br/>";    
29     msg += " tree1: " + tree1.height + "m<br/>";    
30     msg += " tree2: " + tree2.height + "m<br/>";    
31     msg += " tree3: " + tree3.height + "m<br/>";

Finally, we assign our completed msg variable as the Text property of the <asp:label id="Output"/> tag:

33     Output.Text = msg;    
34   }

Ok, so now we've got a class (Tree) and an ASP.NET page that uses it. Let's deploy these on an IIS Web server to try them out!

Create a new directory in your Web root directory (e.g. c:\inetpub\wwwroot) called Trees (i.e. c:\inetpub\wwwroot\Trees). This will be the root directory of our little ASP.NET Web application. Copy PlantTrees.aspx into that directory, then load the page in your browser (http://localhost/Trees/PlantTrees.aspx). Fig. 2 illustrates what you should see.

Compilation error due to a missing classFig. 2 Compilation error due to a missing class

If you sift through the technical mumbo jumbo, you can see that this error screen is complaining that ASP.NET has no idea what a Tree is. We need to put our Tree.dll file (which contains the Tree class) where ASP.NET can find it.

ASP.NET looks for class files in the bin directory of the current Web application. So deploying our Tree class is a two-step process:

  1. Ensure that IIS is configured so that the Trees directory is a Web application.

  2. Place the Tree.dll file into the Trees\bin directory.

To make the Trees directory a Web application, open the Windows Control Panel on your server, open Administrative Tools, then Internet Information Services. Under local computer, Web Sites, Default Web Site, you'll see the Trees directory listed. The plain folder icon next to it indicates that it's just a regular directory, as opposed to a Web application.

Trees as a Web applicationRight-click the Trees directory and choose Properties.... On the Directory page, click the Create button under Application Settings. Click OK to close the Window, and you'll see that Trees now has a nice blue Web Application icon next to it. It looks like a little box with a Web page in it (see Fig. 3).

Now when IIS loads an ASP.NET page in Trees or one of its subdirectories, it will look in Trees\bin for any class files that might be needed. In this case, drop Tree.dll into the bin directory (you'll need to create the bin directory if you haven't already).

With your Web Application created and Tree.dll in the bin directory, try loading http://localhost/Trees/PlantTrees.aspx again. This time, you should see the expected output, as shown in Fig. 4.

Output of PlantTrees.aspxFig. 4 Output of PlantTrees.aspx

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links