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
predefined initial value and is incremented until it reaches a predefined maximum
value. The counter is incremented every time the code in the For loop is executed.
In a For Each loop the variable (we supplied item in the above example) represents
the current object from the given collection. It’s not necessarily an integer, like the
counter in a For loop—it can be any kind of object, including a string, a date, or a
custom object that you created (more about these just a bit later!). The object reference
changes to the next item in the collection each time the code in the For Each loop
executes. So if we were looping over an array of string values, the variable item
would start by containing the string value for the first item in the array, then it
would receive the next item of the array, and so on, until there were no items left
in the array.
You may also come across instances in which you need to exit a loop prematurely.
In these cases, you can use Exit, if your code is in VB, or the equivalent (break)
statement in C#, to terminate the loop:
Visual Basic
Dim i As Integer
For i = 0 To 10
If (i = 5) Then
Response.Write("Oh no! Not the number 5!!")
Exit For
End If
Next
C#
int i;
for (i = 0; i <= 10; i++)
{
if (i == 5)
{
Response.Write("Oh no! Not the number 5!!");
break
;
}
}
Licensed to [email protected]
VB and C# Programming Basics
75
In this case, as soon as our For loop hits the condition i = 5, it displays a warning
message using the Response.Write method (which will be familiar to those with
past ASP experience), and exits the loop so that no further passes will be made
through the loop.
Although we’ve only scratched the surface, VB and C# provide a great deal of power
and flexibility to web developers, and the time you spend learning the basics now
will more than pay off in the future.
Object Oriented Programming Concepts
VB and C# are modern programming languages that give you the tools to write
structured, extensible, and maintainable code. The code can be separated into
modules, each of which defines classes that can be imported and used in other
modules. Both languages are relatively simple to get started with, yet they offer
sophisticated features for writing complex, large-scale enterprise applications.
One of the reasons why these languages are so powerful is that they facilitate
object
oriented programming
(OOP). In this section, we’ll explain the fundamentals of
OOP and learn how adopting a good OOP style now can help you to develop better,
more versatile web applications down the road. This section will provide a basic
OOP foundation angled towards the web developer. In particular, we’ll cover the
following concepts:
■ objects
■ properties
■ methods
■ classes
■ scope
■ events
■ inheritance
In the pages that follow, we’ll discuss these concepts briefly, and from
Chapter 4
onwards, you’ll see some practical examples of OOP in action.
Objects and Classes
So what does “object oriented programming” really mean? Basically, as the name
suggests, it’s an approach to development that puts objects at the center of the proLicensed to [email protected] 76
Build Your Own ASP.NET 3.5 Web Site Using C# & VB
gramming model. The object is probably the most important concept in the world
of OOP; an
object
is a self-contained entity that has
state
and
behavior
, just like a real-world object.
In programming, an object’s state is described by its
fields
and
properties
, while its behavior is defined by its
methods
and
events
. An important part of OOP’s strength comes from the natural way it allows programmers to conceive and design their
applications.
We often use objects in our programs to describe real-world objects—we can have
objects that represent a car, a customer, a document, or a person. Each object has
its own state and behavior.
It’s very important to have a clear understanding of the difference between a class
and an object. A class acts like a blueprint for the object, while an object represents
an instance of the class. I just said that you could have objects of type Car, for example. If you did, Car would be the class, or the type, and we could create as many Car objects as we wanted, calling them myCar, johnsCar, davesCar, and so on.
The class defines the behavior of all objects of that type. So all objects of type Car
will have the same
behavior
—for example, the ability to change gear. However,
each individual Car object may be in a different gear at any particular time; thus,
each object has its own particular
state
.
Let’s take another example: think of Integer (or int) as a class, and age and height
as objects of type Integer. The class defines the behavior of the objects—they’re
numeric, and we can perform mathematical operations on them. The instances of
objects (age and height) have their behavior defined by the class to which they
belong, but they also hold state (so age could be 20).
Take a look at the following code:
Visual Basic
Dim age As Integer
Dim name As String
Dim myCar As Car
Dim myOtherCar As Car
Licensed to [email protected]
VB and C# Programming Basics
77
C#
int age;
string name;
Car myCar;
Car myOtherCar;
As you can see, the syntax for declaring an object is the same as that for declaring
a simple integer or string variable. In C#, we first mention the type of the object,
then we name that particular instance. In VB, we use the Dim keyword.
Object oriented programming sounds like an advanced topic, but getting started
with it is actually very easy, because OOP offers us a natural way to conceive and
design programs. Instead of writing long functions of code to perform specific tasks,
OOP allows us to group pieces of related functionality into classes that we can reuse
over and over, or even extend to incorporate new features. In OOP, one thinks of
programming problems in terms of objects, properties, and methods. And, as we’ve
seen, the best way to get a handle on these terms is to consider a real-world object
and imagine how it might be represented in an OOP program. For the examples that
follow, we’ll use as our example my dog, an Australian Shepherd named Rayne.
Rayne is your average great big, friendly, loving, playful mutt. You might describe
him in terms of his physical properties: he’s gray, white, brown, and black; he stands
roughly one-and-a-half feet high; and he’s about three feet long. You might also
describe some methods to make him do things: he sits when he hears the command
“Sit,” lies down when he hears the command “Lie down,” and comes when his
name is called.
So, if we were to represent Rayne in an OOP program, we’d probably start by creating
a class called Dog. A class describes how certain types of objects look from a programming point of view. When we define a class, we must define the following two items:
Properties
Properties hold specific information that’s relevant to that class of
object. You can think of properties as characteristics of the objects
that they represent. Our Dog class might have properties such as
Color, Height, and Length.
Licensed to [email protected]
78
Build Your Own ASP.NET 3.5 Web Site Using C# & VB
Methods
Methods are actions that objects of the class can be told to perform.
Methods are
subroutines
(if they don’t return a value) or
functions
(if they do) that are specific to a given class. So the Dog class could
have methods such as Sit, and LieDown.
Once we’ve defined a class, we can write code that creates objects of that class, using
the class a little like a template. This means that objects of a particular class expose
(or make available) the methods and properties defined by that class. So, we might
create an instance of our Dog class called rayne, set its properties accordingly, and
use the methods defined by the class to interact with rayne, as shown in
Figure 3.6
. Figure 3.6. An instance of Dog
This is just a simple example to help you visualize what OOP is all about. In the
next few sections, we’ll cover properties and methods in greater detail, and talk
about classes and class instances, scope, events, and inheritance.
Properties
As we’ve seen, properties are characteristics shared by all objects of a particular
class. In the case of our example, the following properties might be used to describe
any given dog:
■ color
■ height
■ length
Licensed to [email protected]
VB and C# Programming Basics
79
In the same way, the more useful ASP.NET Button class exposes properties including:
■ Width
■ Height
■ ID
■ Text
■ ForeColor
■ BackColor
Unfortunately, if I get sick of Rayne’s color, I can’t change it in real life. However,
if Rayne was a .NET object, we could change any of his properties in the same way
that we set variables (although a property can be read-only or write-only). For instance, we could make him brown very easily:
Visual Basic
rayne.Color = "Brown"
C#
rayne.Color = "Brown";
In this example, we’re using an instance of our Dog class called rayne. We use the
dot operator (.) to access the Color property that the object exposes, and set it to
the string "Brown."
Methods
Within our dog example, we can expect to make a particular dog do things by calling
commands. If I want Rayne to sit, I tell him to sit. If I want Rayne to lie down, I tell
him to lie down. In object oriented terms, I tell him what I want him to do by calling
a predefined command or method, and an action results. For example, if we wanted
to make Rayne sit, we would use the following code to call his Sit method:
Visual Basic
rayne.Sit()
Licensed to [email protected]
80
Build Your Own ASP.NET 3.5 Web Site Using C# & VB
C#
rayne.Sit();
Given that rayne is an instance of our Dog class, we say that the Sit method is exposed by the Dog class.
Classes
You can think of a class as a template for building as many objects of a particular
type as you like. When you create an instance of a class, you’re creating an object
of that class, and that new object will have all the characteristics and behaviors
(that is, properties and methods) defined by the class.
In our dog example, rayne was an instance of the Dog class, as
Figure 3.6
illustrated. In our code, we can create a new instance of the Dog class called rayne, as shown
below:
Visual Basic
Dim rayne As New Dog()
C#
Dog rayne = new Dog();
Constructors
Constructors are special kinds of methods are that used to initialize the object. In
OOP, when we create new instances of a class, we say we’re
instantiating
that class.
The constructor is a method of a class that’s executed automatically when a class
is instantiated.
At least one constructor will be defined for most of the classes you will write (though
we can define more than one constructor for a class, as we’ll see shortly), since it’s
likely that some data will need to be initialized for each class at the time of creation.
In C# and VB, the constructor is defined as a method that has the same name as the
class, and has no return type.
Licensed to [email protected]
VB and C# Programming Basics
81
Scope
You should now understand programming objects to be entities that exist in a program and are manipulated through the methods and properties they expose. However, in some cases, we want to create for use inside our class methods that are not
available to code outside that class.
Imagine we’re writing the Sit method inside this class, and we realize that before