Read Build Your Own ASP.NET 3.5 Website Using C# & VB Online
Authors: Cristian Darie,Zak Ruvalcaba,Wyatt Barnett
Tags: #C♯ (Computer program language), #Active server pages, #Programming Languages, #C#, #Web Page Design, #Computers, #Web site development, #internet programming, #General, #C? (Computer program language), #Internet, #Visual BASIC, #Microsoft Visual BASIC, #Application Development, #Microsoft .NET Framework
the dog can sit, it has to shuffle its back paws forward a little (bear with me on this
one!). We could create a method called ShufflePaws, then call that method from
inside the Sit method. However, we don’t want code in an ASP.NET page or in
some other class to call this method—it’d just be silly. We can prevent that from
happening by controlling the scope of the ShufflePaws method.
The careful control of which members of a class are accessible from outside that
class is fundamental to the success of object oriented programming. You can control
the visibility of a class member using a special set of keywords called
access modi-
fiers
:
Public
Defining a property or method of a class as public allows that property
or method to be called from outside the class itself. In other words,
if an instance of this class is created inside another object (remember,
too, that ASP.NET pages themselves are objects), public methods and
properties are freely available to the code that created that instance
of the class. This is the default scope for VB and C# classes.
Private
If a property or method of a class is private, it cannot be used from
outside the class itself. So, if an instance of this class is created inside
an object of a different class, the creating object has no access to
private methods or properties of the created object.
Protected
A protected property or method sits somewhere between public and
private. A protected member is accessible from the code within its
class, or to the classes derived from it. We’ll learn more about derived
classes a bit later.
Deciding which access modifier to use for a given class member can be a very difficult decision—it affects not only your class, but also the other classes and programs that use your class. Of special importance are the class’s public members, which
Licensed to [email protected]
82
Build Your Own ASP.NET 3.5 Web Site Using C# & VB
together form the class’s
public interface
. The public interface acts like a contract
between your class and the users of your class, and if it’s designed properly, it
shouldn’t change over time. If, for example, you mark the Sit method as public,
and later decide to make it private, all the other classes that use this method will
have to change accordingly, which is not good. For an extreme scenario, imagine
that in a year’s time, Microsoft decided to remove the ToString method from its
classes—obviously, this would wreak havoc with your code.
Keep Everything Private Until You Need It
As a simple guideline for designing your classes, remember that it’s often easier
just to make all the members private, and make public only those that really need
to be public. It’s much easier to add to a public interface than it is to remove from
it.
Events
We’ve covered events in some depth already. To sum up, events occur when a
control object sends a message as a result of some change that has been made to it.
Generally, these changes occur as the result of user interaction with the control via
the browser. For instance, when a button is clicked, a Click event is raised, and we
can handle that event to perform some action. The object that triggers the event is
referred to as the
event sender
, while the object that receives the event is referred
to as the
event receiver
. You’
ll learn more about these objects in Chapter 4.
Understanding Inheritance
The term
inheritance
refers to the ability of a specialized class to refine the properties and methods exposed by another, more generalized class.
In our dog example, we created a class called Dog, then created instances of that
class to represent individual dogs such as Rayne. However, dogs are types of animals,
and many characteristics of dogs are shared by all (or most) animals. For instance,
Rayne has four legs, two ears, one nose, two eyes, and so on. It might be better, then,
for us to create a base class called Animal. When we then defined the Dog class, it
would inherit from the Animal class, and all public properties and methods of
Animal would be available to instances of the Dog class.
Similarly, we could create a new class based on the Dog class. In programming
circles, this is called
deriving a subclass
from Dog. For instance, we might create a
Licensed to [email protected]
VB and C# Programming Basics
83
class for Rayne called AustralianShepherd, and one for my other dog, Amigo,
called Chihuahua, both of which would inherit the properties and methods of the
Dog base class, and define new classes specific to each breed.
Don’t worry too much if this is still a little unclear. The best way to appreciate inheritance is to see it used in a real program. The most obvious use of inheritance in ASP.NET is in the technique called code-behind, and we’ll build plenty of ex
amples using inheritance and code-behind in Chapter 4.
Objects in .NET
If this is the first book in which you’ve read about object oriented programming,
you’re probably starting to dream about objects! Don’t worry, the effects of first exposure to objects doesn’t usually last for more than a week. Even though this is yet another discussion about objects, I promise it won’t be boring. Moreover, in the
course of this section, we’ll cover some important concepts that every serious .NET
programmer must know.
So far, we’ve explored various concepts that apply in one form or another to almost
any truly object oriented language. Every language has its peculiarities, but the
general concepts are the same in all of these languages.
You may already have heard the common mantra of object oriented programmers:
“everything is an object.” This has two meanings. First of all, in C#, every program
consists of a class. In all stages of application development, from design to implementation, decisions must be made in regard to the way we design and relate objects and classes to each other. Yes, objects are everywhere.
.NET extends this concept to yet another level, giving the phrase “everything is an
object” extra meaning. In the world of .NET, every class ultimately derives from a
base class named Object, so “everything is an object” becomes “everything is an
Object.”
If you look at the documentation for the ASP.NET Page class, you can see the list
of classes from which this class inherits, as shown in
Figure 3.7
. Licensed to [email protected]
84
Build Your Own ASP.NET 3.5 Web Site Using C# & VB
Figure 3.7. The Page class’s documentation
You’ll remember from the last section that we said our hypothetical
AustralianShepherd class would inherit from the more general Dog class, which,
in turn, would inherit from the even more general Animal class. This is exactly the
kind of relationship that’
s being shown in Figure 3.7
—Page inherits methods and properties from the TemplateControl class, which in turn inherits from a more
general class called Control. In the same way that we say that an Australian Shepherd is an Animal, we say that a Page is a Control. Control, like all .NET classes, inherits from Object.
Since Object is so important that every other class derives from it, either directly
or indirectly, it deserves a closer look. Object contains the basic functionality that
the designers of .NET felt should be available in any object. The Object class contains
these public members:
■ Equals
■ ReferenceEquals
■ GetHashCode
■ GetType
■ ToString
Licensed to [email protected]
VB and C# Programming Basics
85
The only member we’re really interested in at this moment is ToString, which returns the text representation of an object. This method is called automatically when conversions to string are needed, as is the case in the following code, which joins
a number and a string:
Visual Basic
Dim age As Integer = 5
Dim message As String = "Current Age: " & age
C#
int age = 5;
string message = "Current Age: " + age;
Namespaces
As ASP.NET is part of the .NET Framework, we have access to all the goodies that
are built into it in the form of the .NET Framework Class Library. This library represents a huge resource of tools and features in the form of classes, which are organized in a hierarchy of namespaces. When we want to use certain features that .NET
provides, we have only to find the namespace that contains the desired functionality,
and import that namespace into our ASP.NET page. Once we’ve done that, we can
make use of the .NET classes in that namespace to achieve our own ends.
For instance, if we wanted to access a database from a page, we would import the
namespace that contains classes for this purpose, which could be
System.Data.SqlClient. You can view the namespace of a class when visiting its
page in the .NET documentation. For example, the Button control’s class can be
found in System.Web.UI.WebControls.
To use a class that’s part of a namespace, but which isn’t available to you by default,
you either need to import the namespace, for example System.Web.UI.WebControls,
or reference the class using its
fully qualified name
, such as System.Web.UI.WebControls.Button. To import a namespace page, we use the Imports directive in VB, and using in C#:
Licensed to [email protected]
86
Build Your Own ASP.NET 3.5 Web Site Using C# & VB
Visual Basic
Imports System.Data.SqlClient
C#
using System.Data.SqlClient;
As we’ve imported that namespace, we have access to all the classes that it contains.
Using Code-behind Files
Most companies that employ web development teams usually split projects into
two groups—visual design and functional development—because software engineers
are usually poor designers, and designers are often poor engineers. However, the
two groups still need to contribute code and markup to the project. The best approach
is to keep programmers’ code separate from the designers’ page markup as much as
possible. Some of the ASP.NET pages we’ve worked on so far have contained code
render blocks that place VB or C# code directly into the ASP.NET page. The problem
with this approach is that there’s no separation of the presentational elements of
the page from the application logic. The old versions of ASP were infamous for