VisualBasic classes can contain public and private methods, and also can contain data. In this sample chapter from Visual Basic Design Patterns: VB 6.0 and VB.NET, you'll discover how to use Property methods for setting and fetching data -- and why you'd want to do so.

This chapter is from the book

This chapter is from the book

The original versions of Visual Basic (1.0 through 3.0) did not contain much in the way of object-oriented features, and many
programmers' habits were formed by the features of these early versions. However, starting with Visual Basic 4.0, you could
create Class modules as well as Form modules, and use them as objects. In this chapter we'll illustrate more of the advantages
of using class modules. In the following chapter we'll extend these concepts for the more fully object-oriented VB.NET.

A Simple Temperature Conversion Program

Suppose we wanted to write a visual program to convert temperatures between the Celsius and Fahrenheit temperature scales.
You may remember that water freezes at 0° on the Celsius scale and boils at 100°, whereas on the Fahrenheit scale, water freezes
at 32° and boils at 212°. From these numbers you can quickly deduce the conversion formula that you may have forgotten.

The difference between freezing and boiling on one scale is 100° and on the other 180° or 100/180 or 5/9. The Fahrenheit scale
is “offset” by 32, since water freezes at 32° on its scale. Thus,

and

In our visual program, we'll allow the user to enter a temperature and select the scale to convert it, as we see in Figure 3-1.

The preceding program is extremely straightforward and easy to understand and is typical of how many VB programs operate. However, it has some disadvantages that we might want to improve on.

The most significant problem is that the user interface and the data handling are combined in a single program module, rather
than being handled separately. It is usually a good idea to keep the data manipulation and the interface manipulation separate
so that changing interface logic doesn't impact the computation logic and vice versa.