Classes are practically synonymous with objects and they’re at the very heart of programming with Visual Basic. The controls you use to build the visible interface of your application are objects, and the process of designing forms consists of setting the properties of these objects, mostly with point-and-click operations. The Framework itself is an enormous… [Continue Reading]
Custom Classes
Classes and Objects
When you create a variable of any type, you’re creating an instance of a class. The variable lets you access the functionality of the class through its properties and methods. Even the base data types are implemented as classes (the System.Integer class, System.Double, and so on). An integer value, such as 3, is an instance… [Continue Reading]
What Is a Class?
A class is a program that doesn’t run on its own; it’s a collection of methods that must be used by another application. We exploit the functionality of the class by creating a variable of the same type as the class and then call the class’s properties and methods through this variable. The methods and… [Continue Reading]
Building Your Own Class – The Minimal Class Example
Our first example is the Minimal class; we’ll start with the minimum functionality class and keep adding features to it. The name of the class can be anything. Just make sure that it’s suggestive of the class’s functionality. A class might reside in the same file as a form, but it’s customary to implement custom… [Continue Reading]
Adding Code to the Minimal Class – Building Custom Classes
Let’s add some functionality to our bare-bones class. We’ll begin by adding two trivial properties and two methods to perform simple operations. The two properties are called strProperty (a string) and dblProperty (a double). To expose these two members as properties, you can simply declare them as Public variables. This isn’t the best method of… [Continue Reading]
Using Property Procedures when Building Custom Classes
The strProperty and dblProperty properties will accept any value, as long as the type is correct and the value of the numeric property is within the acceptable range. But what if the generic properties were meaningful entities, such as email addresses, ages, or zip codes? We should be able to invoke some code to validate… [Continue Reading]
Customizing Default Members when Building Custom Classes
As you recall, when you created the Minimal class for the first time, before adding any code, the class already exposed a few members— the default members, such as the ToString method (which returns the name of the class) and the Equals method (which compares two objects for reference equality). You can (and should) provide… [Continue Reading]
Custom Enumerations
Let’s add a little more complexity to our class. Because we’re storing birth dates to our custom objects, we can classify persons according to their age. Most basic developers will see an opportunity to use constants here. Instead of using constants to describe the various age groups, we’ll use an enumeration with the following group… [Continue Reading]
Object Constructors
Let’s switch to a few interesting topics in programming with objects. Objects are instances of classes, and classes are instantiated with the New keyword. The New keyword can be used with a number of arguments, which are the initial values of some of the object’s basic properties. To construct a rectangle, for example, you can… [Continue Reading]
Using a Project’s Class in Other Projects
The projects we built in this section are Windows applications that contain a Class module. The class is contained within the project, and it’s used by the project’s main form. What if you want to use this class in another project? First, you must change the type of the project. A Windows project can’t be… [Continue Reading]