Building classes and using them in your code is fairly simple, but there are a few points about Object-Oriented Programming (OOP) that can cause confusion. To help you make the most of Object-Oriented Programming and get up to speed, I’m including a list of related topics that are known to cause confusion to programmers— and… [Continue Reading]
Working with Objects
Objects versus Object Variables in Visual Basic 2008
All variables that refer to objects are called object variables. (The other type of variables are value variables, which store base data types, such as characters, integers, strings, and dates.) In declaring object variables, we usually use the New keyword, which is the only way to create a new object. If you omit this keyword… [Continue Reading]
Properties vs Fields / Shared vs Instance Members in VB 2008
Properties versus Fields When you set or read a property’s value, the corresponding Get or Set segment of the Property procedure is executed. The following statement invokes the Property Set segment of the EMail public property of the class: cust.EMail = “[email protected]” Obviously, every time you call one of the class’s properties, the corresponding public… [Continue Reading]
Type Casting – CType(), DirectCast() – Visual Basic 2008
The data type used most in earlier versions of the language up to VB 6 was the Variant (which was replaced in subsequent versions by the Object type). A variable declared as Object can store anything, and any variable that hasn’t been declared explicitly is an Object variable. Even if you turn on the Strict… [Continue Reading]
Early versus Late Binding in Visual Basic 2008
Untyped variables can’t be resolved at compile time; these variables are said to be late-bound. An expression such as the following can’t be resolved at compile time because the compiler has no way of knowing whether the object retrieved from the ListBox control is of the Customer type (or any other type that exposes the… [Continue Reading]
Inheritance – How to Apply Inheritance – Visual Basic 2008
Here’s a scenario we’re all too familiar with: You’ve written some code, perhaps a collection of functions, which you want to reuse in another project. The key word here is reuse: write once, use many times. For years, VB developers were reusing code, even sharing it with others, with a very simple method: copying from… [Continue Reading]
Inheriting Existing Classes – Inheritance in Visual Basic 2008
To demonstrate the power of inheritance, we’ll extend an existing class: the ArrayList class. This class comes with the Framework and is a dynamic array. (See Chapter, “Storing Data in Collections,” for a detailed description of the ArrayList class.) The ArrayList class maintains a list of objects, similar to an array, but it’s dynamic. The… [Continue Reading]
Inheriting Custom Classes – Inheritance in Visual Basic 2008
In this example, we’ll tackle a very real problem by using inheritance. Consider a structure for storing product information; in most applications, this structure is optimized for a specific product type. In my experience, I’ve seen designs that try to capture the “global” product: a structure that can store products of any type. This approach… [Continue Reading]
Polymorphism in Visual Basic 2008
A consequence of inheritance is another powerful OOP technique: polymorphism, which is the capability of a base type to adjust itself to accommodate many different derived types. Let’s make it simpler by using some analogies in the English language. Take the word run, for example. This verb can be used to describe what athletes, cars,… [Continue Reading]
Advantages of Implementing Polymorphism in Visual Basic 2008
In this section, you’ll build a few classes to represent shapes to demonstrate the advantages of implementing polymorphism. Let’s start with the example Shape class which we are going to discuss here, which will be the base class for all other shapes. This is a really simple class that’s pretty useless on its own. Its… [Continue Reading]