VB.NET and C# Programming Basics: Part I

Object-oriented Programming Concepts

.NET 101

LANGUAGES: VB.NET | C#

ASP.NET VERSIONS: All

 

VB.NET and C# Programming Basics: Part I

Object-oriented Programming Concepts

 

By Zak Ruvalcaba

 

VB.NET and C# are great programming languages because they offer a structured way of programming. By structured, I mean that code is separated into modules, where each module defines classes that can be imported and used in other modules. Both languages are relatively simple to get started with, yet offer features sophisticated enough for complex, large-scale enterprise applications.

 

Each language's ability to support more complex applications (its scalability) stems from the fact that it is an object-oriented programming (OOP) language. But ask seasoned developers what OOP really is, and they'll start throwing out buzzwords and catchphrases that are sure to confuse you - terms like polymorphism, inheritance, and encapsulation. In this article I'm going to explain the fundamentals of OOP and how good OOP style can help you develop better, more versatile Web applications down the road. This article will provide a basic OOP foundation that targets Web developers. In particular, we'll cover the following concepts:

  • Objects
  • Properties
  • Methods
  • Classes
  • Scope
  • Events
  • Inheritance

 

Objects

In OOP, one thinks of programming problems in terms of objects, properties, and methods. The best way to get a handle on these terms is to consider a real-world object and show how it might be represented in an OOP program. Many books use the example of a car to introduce OOP. I'll try to avoid that analogy and use something friendlier: 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, stands roughly one and a half feet high, and is 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 things:

  • Properties. Properties hold specific information 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.
  • 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 lie_down.

 

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 1.

 


Figure 1: The methods defined by the class interact with the object.

 

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 even 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

 

In the same way, the more useful ASP.NET Button class exposes properties, including:

  • Width
  • Height
  • ID
  • Text
  • ForeColor
  • BackColor

 

Unfortunately for me, if I get sick of Rayne's color, I can't change it. ASP.NET objects, on the other hand, let us change their properties very easily in the same way that we set variables. For instance, we've already used properties when setting text for the Label control, which is actually an object of the Label class in the System.Web.UI.WebControls namespace:

 

(VB.NET)

lblMyText.Text = "Hello World"

 

(C#)

lblMyText.Text = "Hello World";

 

In this example we're using a Label control called lblMyText. Remember, ASP.NET is all about controls, and, as it's built on OOP, all control types are represented as classes. In fact, all interaction with ASP.NET pages is handled via controls. When we place a control on a page, we give it a name through its id attribute, and this ID then serves as the name of the control. Rayne is an object. His name, or ID, is Rayne. Rayne has a height of 18. The same holds true for the Label control. The Label control's name or ID in the previous example is lblMyText. Next, we use the dot operator (.) to access the Text property that the object exposes and set it to the string "Hello World."

 

Methods

With our dog example, we can 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 a resulting action is performed. In VB.NET or C#, we would write this as rayne.Sit, or rayne.LieDown.

 

As Web developers, we frequently call methods when a given event occurs. For instance, we can take information from an Access database and create an object called objConn to represent the connection to the database. We then open the connection by calling the Open method on that object as follows:

 

(VB.NET)

Dim objConn As New OleDbConnection( _

  "Provider=Microsoft.Jet.OLEDB.4.0;" & _

  "Data Source=C:\Database\books.mdb")

...

objConn.Open()

 

We say that the Open method is exposed by the connection object, and that we're calling the Open method on the OleDbConnection object stored in objConn. We don't need to know what dark secrets the method uses to do its magic; all we need to know is its name and its purpose.

 

Classes

You can think of a class as a template for building as many objects as you like of a particular type. When you create an instance of a class, you are creating an object of that class, and the new object has all the characteristics and behaviors (properties and methods) defined by the class.

 

In our dog example, Rayne was an instance of the Dog class, as shown in Figure 2.

 


Figure 2: A class serves as the blueprint for an object.

 

We see that Rayne is an object of class Dog. In our code, we could create a new instance of the Dog class, call it rayne, and use all the properties and methods exposed by the object.

 

In OOP, when we create new instances of a class, we say we're instantiating that class. For instance (no pun intended!), if we need to programmatically create a new instance of the Button control class, we could write the following code:

 

(VB.NET)

Dim myButton As New Button()

 

(C#)

Button myButton = new Button();

 

As you can see, we've essentially created a new object called myButton from the Button class. We can then access the properties and methods that Button exposes through our new instance:

 

(VB.NET)

myButton.Text = "Click Me!"

 

(C#)

myButton.Text = "Click Me!";

 

Scope

You should now have a concept of programming objects as entities that exist in a program and are manipulated through the methods and properties they expose. However, in some cases, we want to create methods to use inside our class, which are not available when that class is used in code. Let's return to the Dog class to illustrate this concept.

 

Imagine we're writing the Sit method inside this class, and we realize that before 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 this by controlling the scope of that method.

 

The two types of scope available in VB.NET and C# that you should know about are:

  • 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 it. This is the default scope in VB.NET and C# classes.
  • Private. If a property or method of a class is private, it cannot be used from outside the class itself. 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.

 

Events

Events occur when a control object sends a message in response to some change that has happened to it. Generally, these changes occur as the result of user interaction with the control in 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; the object that receives the event is referred to as the event receiver.

 

Understanding Inheritance

Inheritance refers to the ability of one class to use properties and methods exposed by another class. In our dog example, we first created a class named Dog, then created instances of that class to represent individual dogs. 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, etc. It might be better, then, for us to create a base class named Animal. When we then define 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 class for Australian Shepherd and one for my other dog Amigo, named Chihuahua, both of which would inherit the properties and methods of the Dog base class, and define new ones 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 comes with the technique of code-behind. Part II will continue with a look at using code-behind to separate code from content.

 

This article is excerpted from Build Your Own ASP.NET Website Using C# & VB.NET. Get this book delivered to your doorstep or request additional sample chapters at http://www.sitepoint.com/books/aspnet1/.

 

Zak Ruvalcaba has been researching, designing, and developing for the Web since 1995. He holds a Bachelor's Degree from San Diego State University and a Master of Science in Instructional Technology from National University in San Diego. Zak has developed Web applications for such companies as Gateway, HP, Toshiba, and IBM. More recently, he's worked as a wireless software engineer developing .NET solutions for Goldman Sachs, TV Guide, The Gartner Group, Microsoft, and Qualcomm. He also lectures for the San Diego Community College District on various technologies and tools, including Dreamweaver and ASP.NET.

 

 

 

 

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish