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 … [Read more...] about Building Custom Classes
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 … [Read more...] about Classes and Objects
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 … [Read more...] about What Is a Class?
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 … [Read more...] about Building Your Own Class – The Minimal Class Example
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 … [Read more...] about Adding Code to the Minimal Class – Building Custom Classes
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 … [Read more...] about Using Property Procedures when Building Custom Classes
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 … [Read more...] about Customizing Default Members when Building Custom Classes
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 … [Read more...] about Custom Enumerations
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 … [Read more...] about Object Constructors
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 … [Read more...] about Using a Project’s Class in Other Projects
Firing Events
In addition to methods and properties, classes can also fire events. It's possible to raise events from within your classes, although not quite as common. Controls have many events because they expose a visible interface and the user interacts … [Read more...] about Firing Events
Instance and Shared Methods
As you have seen in earlier chapters, some classes allow you to call some of their members without first creating an instance of the class. The DateTime class, for example, exposes the IsLeapYear method, which accepts as an argument a numeric value … [Read more...] about Instance and Shared Methods
Discussion of a More Practical Class
This section covers a more-practical class that exposes three methods for manipulating strings. I have used these methods in many projects, and I'm sure many readers will have good use for them — at least one of them. The first two methods are the … [Read more...] about Discussion of a More Practical Class
Converting Numbers to Strings
The Num2String method is far more complicated, but if you can implement it as a regular function, it doesn’t take any more effort to turn it into a method. The listing of Num2String is shown in Listing 6.23. First, it formats the billions in the … [Read more...] about Converting Numbers to Strings
Operator Overloading
In this section you’ll learn about an interesting (but quite optional) feature of class design: how to customize the usual operators. Some operators in Visual Basic act differently on various types of data. The addition operator ( + ) is the most … [Read more...] about Operator Overloading