Article
Object Oriented C# for ASP.NET Developers
A C# Tree
For our first foray into object-oriented programming, I propose to implement the Tree class discussed above in C# and then write an ASP.NET page that uses it to instantiate a couple of Trees and make them grow a little.
Open your text editor of choice and create a new text file called Tree.cs. This file will contain the definition of the Tree class. Type the following (the line numbers are provided for your convenience only, and should not be typed as part of the code):
1 /**
2 * Tree.cs
3 * A simple C# class.
4 */
5
6 public class Tree {
Lines 1-4 are just an introductory comment (/* marks the start of a multi-line comment in C#, while */ marks the end), and will be ignored by the compiler. We begin the actual code on line 6 by announcing our intention to create a class called Tree. The word public indicates that our class may be used by any code in our program (or Web site). Note that I am observing the convention of spelling class names with a capital letter.
7 public int height = 0;
8
Aside from the word public at the start of this line, this looks just like a standard variable declaration. As it would seem, we are declaring an integer variable called height and assigning it a value of zero. Again, it is a matter of convention that variable names are not capitalized. Variables declared in this way just inside a class definition become fields for objects of the class. Fields are variables that behave as properties of a class. Thus, this line says that every object of class Tree will have a field (property) called height that will contain an integer value, and that the initial value of the height field will be zero. The word public indicates that any code in your program (or Web site) can view and modify the value in this field. Later in this article, we'll see techniques for protecting data stored in an object's fields, but for now this will suffice.
That's actually all there is to creating a Tree class that will keep track of its height; however, to make this example at least a little interesting, we'll also implement the Grow method that I mentioned in the previous section. It begins with the following:
9 /**
10 * Grows this tree by 1 meter
11 */
12 public void Grow() {
Let me explain this line one word at a time. The word public once again indicates that the Grow method (operation) is publicly available, meaning that it may be triggered by code anywhere in the program. Methods may also be private, protected, internal, or protected internal, and I'll explain the meaning of each of these options later on. The word void indicates that this method will not return a value. Later on we'll see how to create methods that produce some value as an outcome, and for such methods we would replace void with the type of value to be produced (e.g. int).
Finally, the word Grow is the actual name of the method that is to be created. Note that I am observing the convention of spelling method names starting with an uppercase letter (this .NET convention is different from some other languages, such as Java, where methods are normally not capitalized). The empty parentheses following this word indicate that it is a method we are declaring (as opposed to another field, like height above). Later on we'll see cases where the parentheses are not empty. Finally, the opening brace signifies the start of the block of code that will be executed each time the Grow method of a Tree object is triggered.
13 height = height + 1;
This operation happens to be a simple one. It takes the value of the height field and adds one to it, storing the result back into the height field. Note that we did not need to declare height as a variable in this method, since it has already been declared as a field of the object on line 7 above. If we did declare height as a variable in this method, C# would treat it as a separate variable created anew every time the method was run, and our class would no longer function as expected (try it later if you're curious).
14 }
15 }
The closing brace on line 14 marks the end of the Grow method, while that on line 15 marks the end of the Tree class. After typing all this in, save the file. Your next job is to compile it.
If you installed the Microsoft .NET Framework SDK separately (as opposed to getting it with a product like Visual Studio .NET), you should be able to open a Command Line window and run the C# compiler (by typing csc) from any directory. If you're using the Framework SDK that comes with Visual Studio .NET, you need to launch the special Visual Studio .NET Command Prompt instead (Start | Programs | Microsoft Visual Studio .NET | Visual Studio .NET Tools | Visual Studio .NET Command Prompt).
If you've never used the Command Line before, read my cheat sheet on the subject before proceeding. When you're ready, navigate to the directory where you created Tree.cs and type the following to compile the file:
C:\CSTree>csc /target:library Tree.cs
Assuming you typed the code for the Tree class correctly, a file called Tree.dll is created in the same directory. This is the compiled definition of the Tree class. Any .NET program (or Web page) that you try to create a Tree in will look for this file to contain the blueprint of the object to be created. In fact, that's our next step.