object oriented c# for asp

Upload: sribalaji85

Post on 05-Apr-2018

233 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/31/2019 Object Oriented C# for ASP...

    1/30

    Home

    Forums

    Resources

    Marketplace

    Bookstore

    Courses

    Sitepoint Network

    Hot Topics

    PHP

    Photoshop

    Google

    jQuery

    CSS

    Ruby on Rails

    Web Design

    HTML5 Dev Center

    WordPressMobile

    ByKevin Yank| August 14, 2002 | .NET

    0

    0

    ct Oriented C# for ASP.NET Developers SitePoint http://www.sitepoint.com/c-asp-net-de

    0 9/15/2011

  • 7/31/2019 Object Oriented C# for ASP...

    2/30

    Email

    Print

    There was a time when any Web developer with a basic knowledge of JavaScript could pick up the

    essentials of ASP Web development in a couple of hours. With ASP.NET, Microsofts latest platform for

    Web application development, the bar has been raised. Though tidier and generally more developer-

    friendly, real-world ASP.NET development requires one important skill that ASP did not: Object

    Oriented Programming (OOP).

    The two most popular languages that were used to write ASP scripts, VBScript and JScript, have been retrofitted

    with OOP features to become VB.NET and JScript.NET. In addition, Microsoft has introduced an entirely new

    programming language called C# (C-sharp). Unhindered by clunky syntax inherited from a non-OOP legacy, C#

    is arguably the cleanest, most efficient language for .NET in popular use today.

    In this article, Ill introduce you to the OOP features of C# as they apply to ASP.NET Web development. By the

    end, you should have a strong grasp of exactly what OOP is, and why its such a powerful and important aspect

    of ASP.NET. If youre a seasoned pro when it comes to object oriented programming (for example, if you have

    some Java experience under your belt), you might like to bypass all the theory and skip straight to the section on

    Code-Behind Pages.

    This article is the third in a series on ASP.NET. If youre new to ASP.NET Web development and havent readmy previous articles, check out Getting Started with ASP.NET and ASP.NET Form Processing Basics before

    proceeding.

    Since C# is such a similar language to Java, much of this article is based on my two-part series, Object Oriented

    Concepts in Java. Please therefore accept my apologies if some of the examples seem eerily familiar to longtime

    readers.

    Essential Jargon

    Writing .NET applications (be they Windows desktop applications or ASP.NET Web applications) is all about

    constructing a web of interrelated software components that work together to get the job done. Thesecomponents are called objects.

    There are many different kinds of objects, and in fact a big part of programming in .NET is creating your own

    types of objects. To create a new type of object that you can use in your .NET programs, you have to provide a

    blueprint of sorts that .NET will use to create new objects of this type. This blueprint is called a class.

    Lets look at a conceptual example to help these ideas take

    ct Oriented C# for ASP.NET Developers SitePoint http://www.sitepoint.com/c-asp-net-de

    0 9/15/2011

  • 7/31/2019 Object Oriented C# for ASP...

    3/30

    hold. Say you worked for the National Forestry Commission, and your Web site needed to keep track of a group

    of trees in a forest; specifically, say it needed to keep track of the heights of those trees. Fig 1 shows an example

    of the class and objects that you might create as an ASP.NET programmer working on this site.

    On the left we have a class called Tree. This class defines a type of object a Tree that will serve as the

    blueprint from which all Tree objects will be created. The class itself is not a Tree; it is merely a description of

    what a Tree is, or what all Trees have in common. In this example, our Tree class indicates that all Trees have a

    property called height.

    On the right, we have two actual Tree objects. These are Trees, and they were created based on the blueprint

    provided by the Tree class. These objects are said to be instances of the Tree class, and the process of creating

    them is called instantiation. Thus, we can say that by instantiating the Tree class twice, we have created two

    instances of the Tree class, two objects based on the Tree class, or just two Trees. Notice that in creating these

    objects we have assigned a value to their height property. The first Tree is 2 meters high and the second is 5

    meters high. Although the values of these properties differ, this does not change the fact that both objects are

    Trees. They are simply Trees with different heights.

    Classes dont only define properties of objects; they also define operations that may be performed by those

    objects. Such operations are called methods in object-oriented languages like C#. Continuing with our Tree

    example, we could define a method called Grow in the Tree class. The result of this would be that every Tree

    object would then be able to perform the Grow operation as defined in the class. For instance, performing theGrow operation on a Tree might increase its height property by one metre.

    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):

    view plainprint?

    1 /**
    1.

    2 * Tree.cs
    2.

    3 * A simple C# class.
    3.

    4 */
    4.

    5
    5.

    6 public class Tree {6.

    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.

    view plainprint?

    7 public int height = 0;
    1.

    82.

    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

    ct Oriented C# for ASP.NET Developers SitePoint http://www.sitepoint.com/c-asp-net-de

    0 9/15/2011

  • 7/31/2019 Object Oriented C# for ASP...

    4/30

    definition becomefields 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, well see

    techniques for protecting data stored in an objects fields, but for now this will suffice.

    Thats 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, well also implement the Grow method that I mentioned in the previous

    section. It begins with the following:

    view plainprint?

    9 /**
    1.

    10 * Grows this tree by 1 meter
    2.

    11 */
    3.

    12 public void Grow() {4.

    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 Ill explain the meaning of each of

    these options later on. The word void indicates that this method will not return a value. Later on well see howto 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 well 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.

    view plainprint?

    13 height = height + 1;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 youre curious).

    view plainprint?

    14 }
    1.15 }2.

    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 youre 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).

    ct Oriented C# for ASP.NET Developers SitePoint http://www.sitepoint.com/c-asp-net-de

    0 9/15/2011

  • 7/31/2019 Object Oriented C# for ASP...

    5/30

    If youve never used the Command Line before, read my cheat sheet on the subject before proceeding. When

    youre ready, navigate to the directory where you created Tree.cs and type the following to compile the file:

    view plainprint?

    C:\CSTree>1.

    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, thats our next

    step.

    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, heres a look at the full code for the page:

    view plainprint?

    1
    1.

    2
    2.

    3
    3.

    4 Planting Trees
    4.

    5
    5.

    6 protected void Page_Load(Object Source, EventArgs E)
    6.

    7 {
    7.

    8 string msg = "Let's plant some trees!
    ";
    8.

    9
    9.

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

    12
    12.

    13 msg += "I've created a tree with a height of " +
    13.

    14 tree1.height + " metre(s).
    ";
    14.

    15
    15.

    16 tree1.Grow();
    16.

    17
    17.

    18 msg += "After a bit of growth, it's now up to " +
    18.

    19 tree1.height + " metre(s) tall.
    ";
    19.

    20
    20.

    21 Tree tree2 = new Tree();
    21. 22 Tree tree3 = new Tree();
    22.

    23 tree2.Grow();
    23.

    24 tree3.Grow();
    24.

    25 tree2.Grow();
    25.

    26 tree3.Grow();
    26.

    27 tree2.Grow();
    27.

    28 msg += "Here are the final heights:
    ";
    28.

    29 msg += " tree1: " + tree1.height + "m
    ";
    29.

    30 msg += " tree2: " + tree2.height + "m
    ";
    30.

    31 msg += " tree3: " + tree3.height + "m
    ";
    31.

    ct Oriented C# for ASP.NET Developers SitePoint http://www.sitepoint.com/c-asp-net-de

    0 9/15/2011

  • 7/31/2019 Object Oriented C# for ASP...

    6/30

    32
    32.

    33 Output.Text = msg;
    33.

    34 }
    34.

    35
    35.

    36
    36.

    37
    37.

    38


    38.

    39
    39.

    40 40.

    If you look at the bottom of the code, youll see the HTML section basically just contains a single

    tag. Our Page_Load function is where all the action will happen, and it will use that to display the

    results of our messing around.

    So lets focus on the code within Page_Load:

    view plainprint?

    8 string msg = "Let's plant some trees!
    ";1.

    Here were creating a text string (string) called msg. Well use it to store the message that well eventually tellthe tag to display. To begin with, it contains a little introductory message, with a
    tag at the

    end to create a new line on the page.

    view plainprint?

    10 // Create a new Tree
    1.

    11 Tree tree1 = new Tree();2.

    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 isnt just creating a Tree, its also creating a new variable to store it in. Dont worry if this

    distinction is a little hazy for you at this point; later examples will serve to clarify these concepts significantly.

    Now that weve created a tree, lets do something with it:

    view plainprint?

    13 msg += "I've created a tree with a height of " +
    1.

    14 tree1.height + " metre(s).
    ";2.

    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

    ct Oriented C# for ASP.NET Developers SitePoint http://www.sitepoint.com/c-asp-net-de

    0 9/15/2011

  • 7/31/2019 Object Oriented C# for ASP...

    7/30

    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 "Ive created a tree with a height of 0 metre(s)."

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

    view plainprint?

    16 tree1.Grow();1.

    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

    oftree1 again

    view plainprint?

    18 msg += "After a bit of growth, it's now up to " +
    1.

    19 tree1.height + " metre(s) tall.
    ";2.

    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, well polish off

    this example by creating a couple more Trees and having them grow by different amounts:

    view plainprint?

    21 Tree tree2 = new Tree();
    1.

    22 Tree tree3 = new Tree();
    2.23 tree2.Grow();
    3.

    24 tree3.Grow();
    4.

    25 tree2.Grow();
    5.

    26 tree3.Grow();
    6.

    27 tree2.Grow();
    7.

    28 msg += "Here are the final heights:
    ";
    8.

    29 msg += " tree1: " + tree1.height + "m
    ";
    9.

    30 msg += " tree2: " + tree2.height + "m
    ";
    10.

    31 msg += " tree3: " + tree3.height + "m
    ";11.

    Finally, we assign our completedmsg

    variable as theText

    property of the

    tag:

    view plainprint?

    33 Output.Text = msg;
    1.

    34 }2.

    Ok, so now weve got a class (Tree) and an ASP.NET page that uses it. Lets 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

    ct Oriented C# for ASP.NET Developers SitePoint http://www.sitepoint.com/c-asp-net-de

    0 9/15/2011

  • 7/31/2019 Object Oriented C# for ASP...

    8/30

    PlantTrees.aspx into that directory, then load the page in your browser (http://localhost/Trees

    /PlantTrees.aspx). Fig. 2 illustrates what you should see.

    Fig. 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:

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

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

    To make the Trees directory a Web application, open the Windows Control Panel on your server, open

    Administrative Tools, thenInternet Information Services. Under local computer, Web Sites,Default Web Site,

    youll see the Trees directory listed. The plain folder icon next to it indicates that its just a regular directory, as

    opposed to a Web application.

    ct Oriented C# for ASP.NET Developers SitePoint http://www.sitepoint.com/c-asp-net-de

    0 9/15/2011

  • 7/31/2019 Object Oriented C# for ASP...

    9/30

    Right-click the Trees directory and choose Properties. On theDirectory page,

    click the Create button underApplication Settings. Click OK to close the Window, and youll 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 (youll need to create the bin

    directory if you havent 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.

    Fig. 4 Output ofPlantTrees.aspx

    Inheritance

    One of the strengths of object-oriented programming is inheritance. This feature allows you to create a new class

    that is based on an old class. Lets say your program also needed to keep track of coconut trees, so you would

    need a new class called CoconutTree that kept track of the number of coconuts in each tree. You could write

    the CoconutTree class from scratch, copying all the code from the Tree class that is responsible for tracking the

    height of the tree and allowing the tree to grow, but for more complex classes that could involve a lot of

    duplicated code. What would happen if you later decided that you wanted your Trees to have non-integer

    heights (like 1.5 metres)? You would have to adjust the code in both classes!

    Inheritance allows you to define your CoconutTree class as a subclass of the Tree class, such that it inherits the

    fields and methods of that class in addition to its own. To see what I mean, heres the code for CoconutTree:

    view plainprint?

    1 /**
    1.

    2 * CoconutTree.cs
    2.

    3 * A more complex kind of tree.
    3.

    4 */
    4.

    5
    5.

    ct Oriented C# for ASP.NET Developers SitePoint http://www.sitepoint.com/c-asp-net-de

    0 9/15/2011

  • 7/31/2019 Object Oriented C# for ASP...

    10/30

    6 public class CoconutTree {
    6.

    7 public int numNuts = 0; // Number of coconuts
    7.

    8
    8.

    9 public void GrowNut() {
    9.

    10 numNuts = numNuts + 1;
    10.

    11 }
    11.

    12
    12.

    13 public void PickNut() {
    13.

    14 numNuts = numNuts - 1;
    14.15 }
    15.

    16 }16.

    The code : Tree on line 6 endows the CoconutTree class with a height field and a Grow method in addition to

    the numNuts field (yes, this example was fiendishly conceived to produce a childishly amusing variable name

    what of it?) and the GrowNut and PickNut methods that are declared explicitly for the class. The diagram in Fig.

    5 shows the relationship of our two classes.

    Fig. 5 CoconutTree is a subclass ofTree

    By building up a hierarchical structure of classes with multiple levels of inheritance, you can create powerful

    models of complex Objects with little or no duplication of code (which makes for less typing and easy

    maintenance). Well see a very important example of the power of inheritance for ASP.NET developers before

    the end of this article.

    Compiling Multiple Classes

    You should now have two C# source files in your working directory Tree.cs and CoconutTree.cs. Tree.cs

    can still be compiled on its own as before, but since CoconutTree.cs refers to the Tree class, the compiler needs

    to know where to find that class to successfully compile CoconutTree.cs.

    In most cases, the easiest way to proceed is to compile all your related classes into a single DLL (also known as

    an assembly in .NET terminology). To compile Tree.cs and CoconutTree.cs into a single DLL file called

    trees.dll that will contain the definitions of both classes, type the following command in the directory that

    contains the C# source files:

    view plainprint?

    csc /target:library /out:trees.dll Tree.cs CoconutTree.cs1.

    If the files you want to compile are the only C# source files in the directory, you can use a wildcard as a

    shortcut:

    view plainprint?

    csc /target:library /out:trees.dll *.cs1.

    You can then just plop the trees.dll file in the bin directory of your Web application and it will have access to

    both classes.

    ct Oriented C# for ASP.NET Developers SitePoint http://www.sitepoint.com/c-asp-net-de

    30 9/15/2011

  • 7/31/2019 Object Oriented C# for ASP...

    11/30

    Passing Parameters and Returning Values

    Most of the methods we have looked at so far have been of a special type. Here is a declaration of one such

    method, the PickNut method for the CoconutTree class that we developed above:

    view plainprint?

    public void PickNut() {
    1.

    numNuts = numNuts - 1;
    2. }3.

    What makes this, and the other methods we have looked at so far, special is the fact that it doesnt require any

    parameters, nor does it return a value. As youll come to discover as we look at more practical examples of C#

    classes later in this series, most methods do one or both of these.

    Parameters are pieces of information that must be provided when invoking a function to completely specify the

    action to be taken. For example, if we wanted to make the PickNut method above more powerful, we could give

    it a parameter to indicate the number of nuts to be picked:

    view plainprint?

    public void PickNut(int numberToPick) {
    1.

    numNuts = numNuts - numberToPick;
    2.

    }3.

    In this new version of the PickNut method, we have specified that the function takes an integer (int) parameter,

    the value of which is stored in a variable called numberToPick. The code of the method then uses it as the

    number to be subtracted from the numNuts field. Thus, we can now pick as many nuts as we want from a

    CoconutTree with a single invocation of the PickNut method. Here are a few sample invocations ofPickNut:

    view plainprint?

    CoconutTree ct = new CoconutTree(); // New tree
    1.
    2.

    // Presumably we grow a few nuts first...
    3.


    4.

    ct.PickNut(1); // Picks one nut
    5.

    ct.PickNut(5); // picks five nuts
    6.

    ct.PickNut(0); // Doesn't do anything
    7.


    8.

    int nuts = 10;
    9.

    ct.PickNut(nuts); // Picks ten nuts
    10.


    11.

    ct.PickNut(-1); // Picks -1 nut (??)12.

    As this last line demonstrates, there is a problem with this method. Since it accepts any integer as the number of

    nuts to be picked, there is nothing stopping a program that uses it to pick a negative number of nuts. Looking at

    the code of our method, this would actually just add more nuts to the tree, but we should not allow operations on

    our object that do not make sense in the real world. Another operation that would not make sense would be to

    pick more nuts than there are available for picking on the tree! With the existing code, this would result in our

    tree reporting a negative number of nuts hardly a realistic situation.

    These two problems reveal an important issue when designing methods that require parameters. You should

    always make sure that the value passed to a method makes sense before using it. Even if youre only planning on

    ct Oriented C# for ASP.NET Developers SitePoint http://www.sitepoint.com/c-asp-net-de

    30 9/15/2011

  • 7/31/2019 Object Oriented C# for ASP...

    12/30

    using a class in your own programs, its surprisingly easy to forget what values will and will not cause problems

    when you arent checking the values automatically.

    The following modified version ofPickNut checks the parameter to make sure that it is not negative, and that it

    is no larger than the number of nuts on the tree:

    view plainprint?

    public void PickNut(int numberToPick) {
    1.

    if (numberToPick < 0) return; // Cannot pick negative number
    2.

    if (numberToPick > numNuts) return; // Not enough nuts
    3.

    numNuts = numNuts - numberToPick;
    4.

    }5.

    The return command immediately terminates the method. Thus, the operation of picking the nuts (subtracting

    from the numNuts field) will only occur if both of the conditions in the if statements are false. This ensures that

    our two constraints are met before we allow the picking operation to go ahead.

    One problem still remains, here. How can the code that invokes the PickNut method know whether the picking

    operation was successful? After all, if the picking operation fails because one of the constraints was not satisfied,

    we dont want our program to carry on as if it was able to pick the nuts. To resolve this issue, we must onceagain alter PickNut; this time, we will make it return a value:

    view plainprint?

    public PickNut(int numberToPick) {
    1.

    if (numberToPick < 0) ;
    2.

    if (numberToPick > numNuts) ;
    3.

    numNuts = numNuts - numberToPick;
    4.

    ;
    5.

    }6.

    Not only can methods receive parameter values when they are invoked, but they can also send a value back by

    specifying the value as part of the return command. In this new version of the code, we have replaced the word

    void in the method declaration with the word bool. This indicates that the function will return a Boolean

    (true/false) value when it terminates. In this particular case, we have elected to return true if the picking

    operation succeeded, and false if it failed for any reason. This allows us to structure the code of the program

    that invokes the method as follows:

    view plainprint?

    if (!ct.PickNut(10)) {
    1.

    ErrorLabel.Text = "Error: Could not pick 10 nuts!";
    2.

    return;
    3.}
    4.

    nutsInHand = nutsInHand + 10;5.

    The condition of the if statement calls PickNut with a parameter value of 10, and then checks its return value

    to see if its false (note the ! operator). If it is, an error message is printed out. The return command then

    terminates the Page_Load function immediately, which is a reasonable response to an unexpected error.

    Otherwise, the program proceeds as usual.

    Another common use for return values is to create methods that perform some common calculation and return

    the result for the program to use. There will be plenty more examples in the rest of this series for you to learn

    ct Oriented C# for ASP.NET Developers SitePoint http://www.sitepoint.com/c-asp-net-de

    30 9/15/2011

  • 7/31/2019 Object Oriented C# for ASP...

    13/30

    from.

    Access Modifiers

    As we have already seen, classes should usually be set public to allow code elsewhere in your application to

    make use of it. But there are times when youll want to restrict access to a class; for example, you might only

    want to allow other classes in the same assembly (DLL file) to use it. Thats when youll want to specify a

    different access modifier. Class members, which include fields and methods, can be similarly modified to control

    access to them.

    Here are the access modifiers supported by C#:

    public: any code may access the class or class member.

    private: the class or class member is not accessible outside of the class that contains it (yes, in advanced

    cases, you can put a class inside another class).

    protected: the class or class member is only accessible by the class that contains it, or any subclass of

    that class.

    internal: the class or class member is only accessible by code in the same assembly (DLL).

    protected internal: the class or class member is accessible by code in the same assembly (DLL), or by

    any subclass of the class that contains it.

    If you dont specify any access modifier, C# will default to private.

    Consider the following sample declarations:

    view plainprint?

    int numNuts = 0;
    1.


    2.

    bool PickNut(int numberToPick) {
    3.

    ...
    4.

    }5.

    Since no access modifiers are specified, both of these members will act as if they were declared private. Even

    if the class is declared public, the value of the above numNuts field (assuming it is declared as the field of the

    class) may only be accessed or modified by code inside the same class. Similarly, the PickNut method shown

    above may only be invoked by code elsewhere in the class.

    Distinguishing the situations in which each access control setting is appropriate takes a little experience. Its

    tempting at first to just declare everything public to save yourself the trouble of worrying when something is

    accessible and when it isnt. While this will certainly work, it is definitely not in the spirit of object oriented

    programming. Code that you plan to reuse or distribute, especially, will benefit from being assigned the most

    restrictive access control settings that are appropriate. One reason for this is illustrated in the following section.

    For now, just take note of the choices I make for access modifiers. Ill explain my reasons in each case, and

    before long youll be able to choose your own!

    C# Properties

    Previously, we modified the PickNut method so that it would not accept too high a number, which would cause

    our CoconutTree to think it contained a negative number of coconuts. But there is a much simpler way to

    produce this unrealistic situation:

    ct Oriented C# for ASP.NET Developers SitePoint http://www.sitepoint.com/c-asp-net-de

    30 9/15/2011

  • 7/31/2019 Object Oriented C# for ASP...

    14/30

    view plainprint?

    CoconutTree ct = new CoconutTree();
    1.

    ct.numNuts = -10;2.

    How can we protect object fields like numNuts from being assigned values like this that dont make sense? The

    solution is to make the fields themselves private, and permit access to them using a C# Property.

    A C# Property is a pair of methods that is used to read and write a property of an object. From here on, C#

    Properties will be called simply properties.

    Here is an updated version of our CoconutTree class that makes use of this technique:

    view plainprint?

    1 public class CoconutTree : Tree {
    1.

    2 int numNuts = 0;
    2.

    3
    3.

    4 public void GrowNut() {
    4.

    5 numNuts = numNuts + 1;
    5.

    6 }
    6.7
    7.

    8 public bool PickNut(int numToPick) {
    8.

    9 if (numToPick < 0) return false;
    9.

    10 if (numToPick > numNuts) return false;
    10.

    11 numNuts = numNuts - numToPick;
    11.

    12 return true;
    12.

    13 }
    13.

    14
    14.

    15
    15.

    16
    16.

    17
    17. 18
    18.

    19
    19.

    20
    20.

    21
    21.

    22
    22.

    23
    23.

    26 }24.

    As you can see on line 2, the numNuts field is now private, meaning that only code within this class is allowed to

    access it. The GrowNut and PickNut methods remain unchanged; they can continue to update the numNuts field

    directly (the constraints in PickNut ensure that the value ofnumNuts remains legal). Since we still want code to

    be able to determine the number of nuts in a tree, we have added a public NumNuts property (note thecapitalization, which distinguishes NumNuts the property from numNuts the field a pair ofnumNuts, so to

    speak):

    view plainprint?

    15 public int NumNuts {
    1.

    16 get {
    2.

    17 return numNuts;
    3.

    18 }
    4.

    19 set {
    5.

    ct Oriented C# for ASP.NET Developers SitePoint http://www.sitepoint.com/c-asp-net-de

    30 9/15/2011

  • 7/31/2019 Object Oriented C# for ASP...

    15/30

    20 if (value < 0) return;
    6.

    21 numNuts = value;
    7.

    22 }
    8.

    23 }9.

    The declaration of a property starts off just like a field, with an access modifier (public), the type (int), and the

    name (NumNuts), which is capitalized by convention. After those preliminaries, we define the accessors. The get

    accessoris used to retrieve the value of the property, while the set accessoris used to change it.

    Accessors behave just like methods. The get accessor, which must always return a value of the type assigned to

    the property, in this example simply returns the value of the private numNuts field. Nothing too fancy here. The

    set accessor, however, which is always provided with a variable called value that contains the value that is to be

    assigned to the property, checks that this variable is greater or equal to zero (since we cant have a negative

    number of nuts) before storing it in the numNuts field.

    Even in cases where any value is acceptable, you should make your objects fields private and provide a

    property to access them. Doing this allows your programs to exhibit an important feature of object oriented

    programming called encapsulation.

    Encapsulation means that the internal representation of an object is separated from the interface it presents to

    other objects in your program. In other words, a programmer that uses your class only needs to know whatthepublic methods and properties do, not how they work. The advantage is that you can change how your class

    works to improve performance or add new features without breaking any code that relies on the methods

    provided by your original class.

    For example, if you decided you wanted to represent each coconut as an individual object of class Coconut

    instead of using a single integer variable to keep count, you could make the necessary changes and still have the

    same property and two methods as the implementation above. Old code that was written with the original

    interface in mind would continue to work as before, while new code could take advantage of the new features

    (which would of course be provided by new methods).

    As an exercise, rewrite the Tree class so that it correctly encapsulates its height field with a property.

    Constructors

    A constructor is a special type of method that is invoked automatically when an object is created. Constructors

    allow you to specify starting values for properties, and other such initialization details.

    Consider once again our Tree class; specifically, the declaration of its height field (which should now be

    private and accompanied by a public property):

    view plainprint?

    private int height = 0;1.

    Its the = 0 part that concerns us here. Why should all new trees be of height zero? Using a constructor, we can

    let users of this class specify the initial height of the tree. Heres what it looks like:

    view plainprint?

    private int height;
    1.


    2.

    public Tree(int height) {
    3.

    if (height < 0) this.height = 0;
    4.

    ct Oriented C# for ASP.NET Developers SitePoint http://www.sitepoint.com/c-asp-net-de

    30 9/15/2011

  • 7/31/2019 Object Oriented C# for ASP...

    16/30

    else this.height = height;
    5.

    }6.

    At first glance, this looks just like a normal method. There are two differences, however:

    Constructors never return a value; thus, they dont have a return type (void, int, bool, etc.) in their

    declaration.

    Constructors have the same name as the class they are used to initialize. Since we are writing the Tree

    class, its constructor must also be named Tree.

    So dissecting this line by line, the first line states that we are declaring a public constructor that takes a single

    parameter and assigns its value to an integer (int) variable height. Note that this is not the object field height,

    as we shall see momentarily. The second line checks to see if the height variable is less than zero. If it is, we set

    the height field of the tree to zero (since we dont want to allow negative tree heights). If not, we assign the

    value of the parameter to the field.

    Notice that since we have a local variable called height, we must refer to the height field of the current object

    as this.height. this is a special variable in C# that always refers to the object in which the current code is

    executing. If this confuses you (no pun intended), you could instead name the constructors parameter something

    like newHeight. Youd then be able to refer to the object field simply as height.

    Since the Tree class now has a constructor with a parameter, you must specify a value for that parameter when

    creating a new Tree:

    view plainprint?

    Tree myTree = new Tree(10); // Initial height 101.

    Overloaded Methods

    Sometimes it makes sense to have two different versions of the same method. For example, when we modified

    the PickNut method in the CoconutTree class to require a parameter that specified the number of nuts to pick,we lost the convenience of being able to pick a single nut by just calling PickNut(). C# actually lets you declare

    both versions of the method side by side and determines which one to use by the number and type of the

    parameters passed when the method is called. Methods that are declared with more than one version like this are

    called overloaded methods.

    Heres how to declare the two versions of the PickNut method:

    view plainprint?

    public bool PickNut() {
    1.

    if (numNuts == 0) return false;
    2.

    numNuts = numNuts - 1;
    3.return true;
    4.

    }
    5.


    6.

    public bool PickNut(int numToPick) {
    7.

    if (numToPick < 0) return false;
    8.

    if (numToPick > numNuts) return false;
    9.

    numNuts = numNuts - numToPick;
    10.

    return true;
    11.

    }12.

    ct Oriented C# for ASP.NET Developers SitePoint http://www.sitepoint.com/c-asp-net-de

    30 9/15/2011

  • 7/31/2019 Object Oriented C# for ASP...

    17/30

    One way to save yourself some typing is to notice that PickNut() is actually just a special case ofPickNut(int

    numToPick); that is, calling PickNut() is the same as calling PickNut(1), so you can implement PickNut() by

    simply making the equivalent call:

    view plainprint?

    public bool PickNut() {
    1.

    return PickNut(1);
    2.

    }
    3.
    4.

    public bool PickNut(int numToPick) {
    5.

    if (numToPick < 0) return false;
    6.

    if (numToPick > numNuts) return false;
    7.

    numNuts = numNuts - numToPick;
    8.

    return true;
    9.

    }10.

    Not only does this save two lines of code, but if you ever change the way the PickNut method works, you only

    have to adjust one method instead of two!

    Constructors can be overloaded in the same way as normal methods. If you miss the convenience of being ableto create a new Tree of height zero, you can declare a second constructor that takes no parameters:

    view plainprint?

    private int height;
    1.


    2.

    public Tree() {
    3.

    this(0);
    4.

    }
    5.


    6.

    public Tree(int height) {
    7. if (height < 0) this.height = 0;
    8.

    else this.height = height;
    9.

    }10.

    Note that we have once again saved ourselves some typing by implementing the simpler method (Tree()) by

    invoking a special case of the more complex method (Tree(0)). In the case of a constructor, however, you call it

    by the special name this.

    Advanced Inheritance: Overriding Methods

    I covered inheritance earlier in this article, but I left out one advanced issue for the sake of brevity that Id like

    to cover now: overriding methods. As you know, an object of class CoconutTree inherits all of the features of

    the Tree class, on which it is based (i.e. CoconutTree is a subclass ofTree). Thus, CoconutTrees have Grow

    methods just like Trees do.

    But what if you wanted CoconutTrees to sprout new coconuts when they grew? Sure, you could call the

    GrowNut method every time you caused a CoconutTree to grow, but it would be nicer if you could treat Trees

    and CoconutTrees exactly the same way (i.e. call their Grow method) and have them both do what theyre

    supposed to do when objects of their type grow.

    To have the same method do something different in a subclass, you must override that method with a new

    ct Oriented C# for ASP.NET Developers SitePoint http://www.sitepoint.com/c-asp-net-de

    30 9/15/2011

  • 7/31/2019 Object Oriented C# for ASP...

    18/30

    definition in the subclass. Put simply, you can re-declare the Grow method in the CoconutTree class to make it

    do something different! Heres a new definition for grow that you can add to your CoconutTree class:

    view plainprint?

    public void Grow() {
    1.

    height = height + 1;
    2.

    GrowNut();
    3.

    }4.

    Simple, right? But what if you added new functionality to the Grow method in the Tree class (e.g. to grow

    leaves)? How could you make sure that this was still inherited by the CoconutTree class without having to make

    the change in both places? Like in our discussion of overloaded methods, where we implemented a simple

    method by calling a special case of the more complicated method, we can implement a new definition for a

    method in a subclass by referring to its definition in the base class (also called the parent class or superclass):

    view plainprint?

    public void Grow() {
    1.

    base.Grow();
    2.

    GrowNut();
    3.}4.

    The base.Grow() line invokes the version ofGrow defined in the base class (Tree), thus saving us from having

    to reinvent the wheel. This is especially handy when you are creating a class that extends a class for which you

    do not have the source code (e.g. a class provided by another developer, or built into .NET). By simply calling

    the base class versions of the methods you are overriding, you can ensure that your objects arent losing any

    functionality.

    Constructors may be overridden just like normal methods, but calling the version of the constructor in the base

    class is slightly different. Heres a set of constructors for the CoconutTree class, along with the new declaration

    of the numNuts field without an initial value:

    view plainprint?

    private int numNuts;
    1.


    2.

    public CoconutTree() : base() {
    3.

    numNuts = 0;
    4.

    }
    5.


    6.

    public CoconutTree(int height) : base(height) {
    7.

    numNuts = 0;
    8.

    }
    9.
    10.

    public CoconutTree(int height, int numNuts) : base(height) {
    11.

    if (numNuts < 0) this.numNuts = 0;
    12.

    else this.numNuts = numNuts;
    13.

    }14.

    The first two constructors override their equivalents in the Tree class, while the third is completely new. Notice

    that we call the constructor of the base class as base(), but instead of putting this call inside the constructor

    body, we add it to the end of the constructor declaration, using the : operator. All three of our constructors call a

    constructor in the base class to ensure that we are not losing any functionality.

    ct Oriented C# for ASP.NET Developers SitePoint http://www.sitepoint.com/c-asp-net-de

    30 9/15/2011

  • 7/31/2019 Object Oriented C# for ASP...

    19/30

    Namespaces

    For much of this article, we have worked with a class called Tree. Now, while Tree isnt an especially original

    name for a class, it fits the class perfectly. The problem is that the same name might fit another class just as well,

    and youll have a naming conflict on your hands. Such conflicts arent too serious when you get to write all your

    own classes; however, when you need to bring in a set of classes that someone else wrote for use in your

    program, things can get messy.

    Consider, for example, what would happen if youd designed all of the classes to handle the logic for a Web sitethat will track the sales of buttons for clothing. In such a case it would be natural to have a class called Button,

    but then your boss tells you he or she wants a nice, graphical user interface for the program. To your dismay, you

    find that the class built into the .NET Framework for creating buttons on user interfaces is called (you guessed it)

    Button. How can this conflict be resolved without having to go back through your code and change every

    reference to your Button class?

    Namespaces to the rescue! C# provides namespaces as a way of grouping together classes according to their

    purpose, the company that wrote them, or whatever other criteria you like. As long as you ensure that your

    Button class is not in the same namespace as C#s built-in Button class, you can use both classes in your

    program without any conflicts arising.

    By default, classes you create reside in the default namespace, an unnamed namespace where all classes that are

    not assigned namespaces go. For most of your programs it is safe to leave your classes in the default namespace.

    All of the .NET Frameworks built-in classes as well as most of the classes you will find available on the Internet

    and from other software vendors are grouped into namespaces, so you usually dont have to worry about your

    classes names clashing with those of other classes in the default namespace.

    You will want to group your classes into namespaces if you intend to reuse them in future projects (where new

    class names may clash with those you want to reuse), or if you want to distribute them for use by other

    developers (where their class names may clash with your own). To place your class in a namespace, you simply

    have to surround your class declaration with a namespace declaration. For example, classes that we develop at

    SitePoint.com are grouped in the Sitepoint namespace by adding the following to our C# files:

    view plainprint?

    namespace SitePoint {
    1.

    // Class declaration(s) go here
    2.

    }3.

    If we were to place our Tree class inside the SitePoint namespace in this way, the class full name would

    become SitePoint.Tree. You can also declare namespaces within namespaces to further organize your classes

    (e.g. SitePoint.Web.Tree).

    As it turns out, the Button class built into the .NET Framework is actually in the System.Windows.Forms

    namespace, which also contains all of the other classes for creating basic graphical user interfaces in .NET. Thus,thefully qualif ied name of .NETs Button class is System.Windows.Forms.Button. To make use of this class

    without your program thinking that youre referring to your own Button class, you can use this full name

    instead. For example:

    view plainprint?

    // Create a Windows Button
    1.

    System.Windows.Forms.Button b = new System.Windows.Forms.Button();2.

    In fact, C# requires that you use the full name of any class that is not in the same namespace as the current

    ct Oriented C# for ASP.NET Developers SitePoint http://www.sitepoint.com/c-asp-net-de

    30 9/15/2011

  • 7/31/2019 Object Oriented C# for ASP...

    20/30

    class!

    But what if your program doesnt have a Button class to clash with the one built into .NET? Spelling out the full

    class name every time means a lot of extra typing. To save yourself this annoyance, you can importthe

    System.Windows.Forms namespace into the current namespace by putting a using line at the top of your C#

    file (just inside the namespace declaration(s), if any):

    view plainprint?

    namespace SitePoint {
    1.

    using System.Windows.Forms;
    2.

    // Class declaration(s) go here
    3.

    }4.

    Once its namespace is imported, you can use the class by its short name (Button) as if it were part of the same

    namespace as your class.

    So if you put your Tree class into a namespace called SitePoint, any class not also declared to be in the

    SitePoint namespace that needed to use it would either have to call it SitePoint.Tree or import the

    SitePoint namespace.

    For code in an ASP.NET page (.aspx file), the following namespaces are automatically imported:

    System

    System.Collections

    System.Collections.Specialized

    System.Configuration

    System.IO

    System.Text

    System.Text.RegularExpressions

    System.Web

    System.Web.Caching

    System.Web.Security

    System.Web.SessionState

    System.Web.UI

    System.Web.UI.HtmlControls

    System.Web.UI.WebControls

    But if you wanted to access the newly-namespaced SitePoint.Tree class in your PlantTrees.aspx file, youd

    either have to call it by its full name, or use an import directive.

    Like a page directive, an import directive is a special tag that goes at the top of your file to provide special

    information about your ASP.NET page. Heres what the import directive to use the SitePoint namespace would

    look like:

    view plainprint?

    1.

    Static Members

    All class members (including methods, fields and properties) can be declared static. Static members belong to

    the class instead of to objects of that class. Since static members arent that common in basic ASP.NET

    development, I wont dwell on static members too heavily; but for the sake of completeness, lets look at a

    ct Oriented C# for ASP.NET Developers SitePoint http://www.sitepoint.com/c-asp-net-de

    30 9/15/2011

  • 7/31/2019 Object Oriented C# for ASP...

    21/30

    simple example.

    It might be useful to know the total number of trees that had been created in our program. To this end, we could

    create a static field called totalTrees in the Tree class, and modify the constructor to increase its value by

    one every time a Tree was created. Then, using a static property called TotalTrees, for which we would only

    define a get accessor (making it a read-only property), we could check the value at any time by checking the

    value ofTree.TotalTrees.

    Heres the code for the modified Tree class:

    view plainprint?

    public class Tree {
    1.


    2.

    private static int totalTrees = 0;
    3.

    private int height;
    4.


    5.

    public Tree() {
    6.

    this(0);
    7.

    }
    8.


    9.public Tree(int height) {
    10.

    if (height < 0) this.height = 0;
    11.

    else this.height = height;
    12.

    totalTrees = totalTrees + 1;
    13.

    }
    14.

    f
    15.

    public static int TotalTrees {
    16.

    get {
    17.

    return totalTrees;
    18.

    }
    19.

    }
    20.
    21.

    // ... rest of the code here ...
    22.

    }23.

    Static members are useful in two main situations:

    When you want to keep track of some information shared by all members of a class (as above).

    When it doesnt make sense to have more than one instance of a property or method.

    As I said, static members dont tend to crop up in basic ASP.NET applications, but its useful to know about

    them so youre not totally bamboozled if you see someone accessing a method in a class instead of an object!

    Code-Behind Pages in ASP.NET

    Okay, so Ive waffled on for what must seem like forever, and all youve learned how to do is grow trees (and

    nuts!). "Whens he going to get to the point?!" you must be asking. Either that, or you already know the basic

    concepts of OOP and youve skipped directly here for the meat.

    Well heres the payoff:

    Everything in ASP.NET is an object.

    ct Oriented C# for ASP.NET Developers SitePoint http://www.sitepoint.com/c-asp-net-de

    30 9/15/2011

  • 7/31/2019 Object Oriented C# for ASP...

    22/30

    Everything including the pages themselves. Every .aspx page you write is automatically compiled into a class

    that inherits from the System.Web.UI.Page class, which is built into the .NET Framework.

    Everything that ASP.NET pages do automatically is handled by that class (e.g. calling the Page_Load function

    which is actually a method before the page is displayed). So creating a site of ASP.NET pages is actually a

    process of creating subclasses ofSystem.Web.UI.Page. This is illustrated in Fig. 6.

    Fig. 6 ASP.NET pages inherit from

    System.Web.UI.Page by default

    Okay, I can see youre getting impatient again. How about I tell you what this gives you (besides a headache)?

    The object-oriented nature of ASP.NET lets you achieve complete separation of design code (HTML) and

    server-side code. The technique for doing this is called Code-Behind. The idea is that you create a subclass ofSystem.Web.UI.Page that contains all your server-side code, and then make your .aspx page (which contains

    all your design code) inherit from thatclass instead of from System.Web.UI.Page (see Fig. 7).

    Fig. 7 Code-Behind files: server-side logic,

    and design code are kept separate

    Not convinced? Lets look at an example.

    Heres the code for the last example we saw in ASP.NET Form Processing Basics:

    view plainprint?


    1.


    2.


    3.

    My First ASP.NET Form
    4.


    5.

    protected void Page_Load(Object Source, EventArgs E) {
    6.

    if (IsPostBack) {
    7.

    NameForm.Visible = false;
    8.

    NameLabel.Text = NameBox.Text;
    9.

    ct Oriented C# for ASP.NET Developers SitePoint http://www.sitepoint.com/c-asp-net-de

    30 9/15/2011

  • 7/31/2019 Object Oriented C# for ASP...

    23/30

    NameLabel.Style.Add( "font-weight", "bold" );
    10.

    }
    11.


    12.

    TimeLabel.Text = DateTime.Now.ToString();
    13.

    }
    14.


    15.


    16.


    17.


    18.

    Hello there
    19.

    !


    20.


    21.


    22.

    Do you have a name?


    23.


    24.


    25.


    26.


    27.

    The time is now:


    28.


    29.
    30.

    31.

    Now, this is a relatively simple ASP.NET page, and yet the server-side code (the Page_Load method) takes up

    about half the page. An HTML-and-JavaScript designer who had to tweak the design of a more typical ASP.NET

    page would suffer a coronary at the sight of the code in the file (many ASP developers had trouble keeping

    designers around for this very reason).

    In addition, if a Web development team composed of server-side developers and Web designers had to work on

    a site collaboratively, the tug-of-war involved in letting team members of both types work on a page at the same

    time would be next to unmanageable!

    Lets create a class called HelloTherePage that contains all the server-side code:

    view plainprint?

    using System;
    1.

    using System.Web;
    2.

    using System.Web.UI;
    3.

    using System.Web.UI.HtmlControls;
    4.

    using System.Web.UI.WebControls;
    5.


    6.

    public class HelloTherePage : Page {
    7. protected HtmlForm NameForm;
    8.

    protected Label NameLabel;
    9.

    protected Label TimeLabel;
    10.

    protected TextBox NameBox;
    11.


    12.

    protected void Page_Load(Object Source, EventArgs E) {
    13.

    if (IsPostBack) {
    14.

    NameForm.Visible = false;
    15.

    NameLabel.Text = NameBox.Text;
    16.

    NameLabel.Style.Add( "font-weight", "bold" );
    17.

    ct Oriented C# for ASP.NET Developers SitePoint http://www.sitepoint.com/c-asp-net-de

    30 9/15/2011

  • 7/31/2019 Object Oriented C# for ASP...

    24/30

    }
    18.


    19.

    TimeLabel.Text = DateTime.Now.ToString();
    20.

    }
    21.

    }22.

    First note the block ofusing commands at the top of the file. Since were not inside a .aspx file anymore, we

    dont have the benefit of all the automatic namespace imports we had before; therefore, we must explicitly

    import the namespaces we want to use. The five namespaces Ive used here are the most common for ASP.NETprograms.

    The Page_Load method is defined exactly as it was in the .aspx page above. Note that it is declared protected,

    since the only class that needs to call this class is the subclass were going to create with our .aspx file (recall

    that protected means a member is accessible only by the class itself or any of its subclasses).

    Also declared in this class are four protected fields: NameForm, NameLabel, TimeLabel, and NameBox. These

    are the IDs of the page elements that Page_Load accesses in the page. In the .aspx subclass, these fields will be

    overridden by the actual elements (e.g. overrides NameLabel), but they must be

    declared in this class so that C# doesnt get confused when we refer to them in Page_Load.

    Since NameLabel and TimeLabel represent tags in the .aspx page, they should be objects of classSystem.Web.UI.WebControls.Label (which we can abbreviate as Label, since our file is using the

    System.Web.UI.WebControls namespace). NameBox is a and is therefore a

    System.Web.UI.WebControls.TextBox. NameForm is an HTML tag, which is represented by an object

    of class System.Web.UI.HtmlControls.HtmlForm. In general, all ASP.NET tags () have their

    corresponding classes in the System.Web.UI.WebControls namespace, while HTML tag classes are in

    System.Web.UI.HtmlControls, and have names of the form HtmlTagName.

    Save this file as HelloThere.cs in the same directory as the .aspx page (dont worry IIS is smart enough not

    to allow Web browsers to view C# and other .NET source code files in Web-accessible directories). Now lets

    re-write HelloThere.aspx to use the Code-Behind file we have just written:

    view plainprint?


    1.


    2.


    3.

    My First ASP.NET Form
    4.


    5.


    6.


    7.

    Hello there
    8.

    !


    9.


    10.
    11.

    Do you have a name?


    12.


    13.


    14.


    15.


    16.

    The time is now:


    17.


    18.


    19.

    ct Oriented C# for ASP.NET Developers SitePoint http://www.sitepoint.com/c-asp-net-de

    30 9/15/2011

  • 7/31/2019 Object Oriented C# for ASP...

    25/30

    20.

    The changes are dramatic, but very simple! Ive simply removed the tag that contained the Page_Load

    method, which is now defined in HelloTherePage, the base class of this page defined in the Code-Behind file,

    and added two new attributes to the Page directive on the first line. I also removed the Language attribute from

    this directive, since there is no longer any server-side code in this file for which a language would need to be

    specified.

    The Inherits attribute is set to the name of the class from which this page should inherit. By default, this isSystem.Web.UI.Page. The src attribute tells the ASP.NET page compiler where to find the source code for the

    class specified. ASP.NET uses this attribute to intelligently compile the Code-Behind file for you on the fly and

    put the resulting .dll file in the bin directory of the Web application, so that this page can find it. You could of

    course do this manually, but its much easier to let ASP.NET do the work of recompiling the Code-Behind file

    whenever you make changes automatically.

    With HelloThere.cs and HelloThere.aspx in the same directory on your Web server, you should be able to

    load HelloThere.aspx as usual and find that it behaves exactly as it did before you split the server-side code

    into a separate file.

    Summary

    Well! Its been a long trip, but in this article Ive taken you on a tour of all the important object-oriented features

    of the C# language. Whats more, Ive shown you how they apply to ASP.NET Web development.

    In particular, Ive demonstrated how to use a class to handle some of the logic in a simple page that tracks the

    growth of trees. In a practical system, each Tree class might for instance fetch its information from a database of

    national forest growth. The point is that the ASP.NET page doesnt need to know how to track the growth of

    trees, because all that functionality is bottled up in a handy class that we can reuse in other projects if needed.

    By far the most exciting application of object oriented programming concepts to everyday ASP.NET

    development, however, is the fact that everything in ASP.NET is an object. By defining a class in a Code-Behind

    file for each of our pages, we can totally separate the server-side logic of our pages from their design, thus lettingour Web designers sleep at night. In fact, you could even make two or more different .aspx pages that inherit

    from the same Code-Behind file in order to experiment with different page designs (perhaps a WAP version of

    your site for mobile devices?) that share the same server-side logic.

    In the next article in this series, Ill introduce you to event-driven programming. It turns out that .NET classes

    (including most of those that make up ASP.NET) can send out events that your code can listen for and react to.

    Well see how these events are used to achieve some common tasks in ASP.NET development.

    See you next time!

    ct Oriented C# for ASP.NET Developers SitePoint http://www.sitepoint.com/c-asp-net-de

    30 9/15/2011

  • 7/31/2019 Object Oriented C# for ASP...

    26/30

    Like

    Written By:

    Kevin Yank

    Kevin began developing for the Web in 1995 and is a highly respected technical author. He wrote Build your

    own Database Driven Website using PHP and MySQL, a practical step-by-step guide published by SitePoint,

    and he's co-author of the SitePoint Tech Times, a bi-weekly newsletter for technically-minded web developers.

    Kev believes that any good webmaster should have seen at least one episode ofMacGyver.

    Website

    >> More Posts By Kevin Yank

    Related Posts

    Object Oriented Concepts in Java Part 2

    Object Oriented Concepts in Java Part 1

    JavaScript Object-Oriented Programming Part 1 Article

    JavaScript Object-Oriented Programming Part 1 Article

    JavaScript Object-Oriented Programming Part 1 Article

    2 people liked this.

    LoginAdd New Comment

    ct Oriented C# for ASP.NET Developers SitePoint http://www.sitepoint.com/c-asp-net-de

    30 9/15/2011

  • 7/31/2019 Object Oriented C# for ASP...

    27/30

    M Subscribe by email S RSS

    Real-time updating is paused. (Resume)

    Showing 0 comments

    Join 1,046,236 followers

    ct Oriented C# for ASP.NET Developers SitePoint http://www.sitepoint.com/c-asp-net-de

    30 9/15/2011

  • 7/31/2019 Object Oriented C# for ASP...

    28/30

    Popular Categories

    Business

    Community

    JavaScript & CSSNews & Trends

    PHP Tutorials & Articles

    Web Tech

    ct Oriented C# for ASP.NET Developers SitePoint http://www.sitepoint.com/c-asp-net-de

    30 9/15/2011

  • 7/31/2019 Object Oriented C# for ASP...

    29/30

    Useful Information

    Contact Us

    Write For Us

    Advertise With Us

    About SitePoint

    Work for SitePoint

    Privacy Policy

    Terms and Conditions

    Best Sellers

    HTML5 & CSS3 for the Real World

    The Web Design Business Kit, 3rd Edition

    The Principles of Beautiful Web Design, 2nd Edition

    The SEO Business Guide

    Build Your Own Wicked WordPress Themes

    ct Oriented C# for ASP.NET Developers SitePoint http://www.sitepoint.com/c-asp-net-de

    30 9/15/2011

  • 7/31/2019 Object Oriented C# for ASP...

    30/30

    The SitePoint Network

    SitePoint

    RubySource

    BuildMobile

    DesignFestival

    CloudSpring

    PHPMaster

    SitePoint Market

    Copyright 1998 - 2011 SitePoint Pty. Ltd., Melbourne, Australia

    ct Oriented C# for ASP.NET Developers SitePoint http://www.sitepoint.com/c-asp-net-de