Ejemplos para Programadoras de Visual Basic::

Creating a Class Module Using Visual Basic 5.0

by Jon Vote
04/00



Introduction:

- This article will describe how to create a Class Module using Visual Basic. The example was written using Visual Basic 5.0.

A Class Module is an object similar to any other object like a Text Box or a Label. Not a visual object like a Text Box is, a Class Module is none the less an object in every sense of the word. The code in a Class Module looks just like the code any Visual Basic programmer is used to with very few exceptions. However, the behavior of the code in a Class Module is different in several key points:

1) Encapsulation. It is a fair bet that very few Visual Basic programmers who have used TextBoxes have ever seen the code that is required underneath the covers to make a TextBox behave in the manner it does. This is an illustration of code encapsulation. The user of the object normally does not know and certainly does not need to know how the object itself works. They only need set the properties, invoke the methods and monitor the events of the object.

2) Reusability: The objects can be thought of as black boxes that perform a specific function on a specific kind of thing. Again, think of a TextBox. A TextBox is a very well designed object. You do not need to copy many lines of code every time you want to provide an area for the user to enter text. You just plunk down a TextBox and that's that.

3) Polymorphism: The idea of polymorphism is actually something that every programmer is familiar with, they just don't necessarily know it. Anyone who has ever used any of the arithmetic operators on more than one data type has taken advantage of polymorphism. To a programmer, it may make perfect sense that the plus sign is used add an integer to an integer, or a single to a single, or a double to an integer. However, the code that runs underneath is very different. With polymorphism, the similarities of the function can be shared, but the differences allowed for.

A Temperature Object

- The Class Module created will be something very useful to anyone who finds themselves out in the middle of the dessert some time and would like to know how to convert 145 degrees Fahrenheit to its Centigrade equivalent. The temperature object will be very simple. There will be two properties, Degrees Fahrenheit and DegreesCentigrade. Changing either one will cause its counterpart to immediately changed to the appropriate value. A small test program will be written to use the object.

A short trip down Algorithm Lane

- Ok, let's get started. We could look up the conversion between F. and C. but hey we're techies! We can figure it out! First of all, let's look at what we know. The boiling point of water is 100 degrees C. and 212 degrees F. Also, the freezing point of water is 0 degrees C. and 32 degrees F. So what does that tell us? It tells me that this would be a good time to go get a cup of tea and work on the problem when we get back... Now, lets see. (I thought about this on the way to getting the cup of tea). There are 100 units from freezing to boiling if you are a C. creature, and 212 - 32 = 180 units if you are a F. creature. This means that for every change of 100 degrees C. we can expect to see a 180 degree change in F - or equivalently; using a little algebra we find that we will want to multiply degrees C. by 1.8 to convert to F. and divide degrees F. by 1.8 to convert to C. We almost have it. Life is good. Let's check it out:

0 degrees C. = 1.8 * 0 = 0 degrees F.???

No. Life is bad. We need to account for the fact that the freezing point on the F. scale is 32 not 0. Let's add 32 to the previous result:

0 degrees C. = 1.8 * 0 + 32 = 32 degrees F.
20 degrees C. = 1.8 * 20 + 32 = 68 degrees F.
100 degrees C. = 1.8 * 100 + 32 = 212 degrees F.

Looks good!

To go the other way, we will need to first subtract the 32 and then plug it in:

32 degrees F. = (32 - 32)/1.8 = 0 degrees C.
68 degrees F. = (68 - 32)/1.8 = 20 degrees C.
212 degrees F. = (212 - 32)/1.8 = degrees C.

Whew! Life is good!

GUI!!

Now we have the algorithm, let's start with the GUI. Fire up VB and start a new project.

1) Change the following Form1 properties:

a) Form1.Caption = "Temperature Conversion Program"
b) Form1.StartupPosition = 2
c) Form1.BorderStyle = 1
d) (Optional) Form1.Icon = (browse to your favorite Icon)

2) Add a text box with the following properties:

a) Text1.Text = ""
b) Text1.Alignment = Right
c) Text1.Width = 615

3) Add a label with the following properties:

a) Label1.Caption = ""
b) Label1.Borderstyle = Fixed
c) Label1.Width = 4455

4) Add an array of two option buttons. (To do this, place one option button on the form. Click it and select Edit|Copy from the menu. Now select Edit|Paste and answer yes when asked if you want to create a control array.) Set the properties as follows:

a) Option(0).Caption = "Fahrenheit to Centigrade"
b) Option(1).Caption = "Centigrade to Fahrenheit"
c) Option(0).Value = true

5) Last, add a command button with the following properties:

a) Command1.Caption = "Convert"
b) Command1.Default = True

The user will enter the number of degrees into Text1. Clicking Command1 (or pressing the enter key) will convert the value as indicated by the option selected.

Time for Class

Now it's time to build the Temperature class. From the menu, select:

Project|Add Class Module

and choose Class Module from the list of choices.

Press F4 to bring up the properties window, and change the name of the class from Class1 to Temperature.

Remembering the algorithms above, let's write functions to convert back and forth between Fahrenheit and centigrade. These are pretty simple:

Private Function CtoF(C As Single) As Single 
'Convert Fahrenheit to Centigrade 

CtoF = C * 1.8 + 32 

End Function 

Private Function FtoC(F As Single) As Single 
'Convert Centigrade to Fahrenheit 

FtoC = (F - 32) / 1.8 

End Function 

- The word 'Private' in front of the Function definition tells the compiler that these functions may only be used within the class itself. They won't be known to the outside world. The interface with the outside world will require a Public declaration. - We could use a publicly declared variable to do this, but this is not a good programming style. It's much better not to let the user of the object mess with the object's variables directly. Also, in the case of this particular object, setting one of the values effects the other. We want the one property's value to change immediately at the time its counterpart does. The proper way to do this is with Property Let and Property Get. This is a bit more complicated than just setting a global variable, but it will give us the benefit of enforcing the relationship between the two properties. We will use two global variables to represent the temperature: m_DegreesFahrenheit and m_DegreesCentigrade. These variables will only be known within the class itself. The outside world will interface with the object using the public properties DegressFahrenheit and DegreesCentigrade. Add the global variable declarations to the Temperature class module as follows:

Private m_DegreesFahrenheit As Single 
Private m_DegreesCentigrade As Single 

Now, the code for the Property Let and PropertyGet statements can be added:

Public Property Let DegreesFahrenheit(ByVal SetVal As Single) 

m_DegreesFahrenheit = SetVal 
m_DegreesCentigrade = FtoC(SetVal) 

End Property 

Public Property Get DegreesFahrenheit() As Single 

DegreesFahrenheit = m_DegreesFahrenheit 

End Property 

Public Property Let DegreesCentigrade(ByVal SetVal As Single) 

m_DegreesCentigrade = SetVal 
m_DegreesFahrenheit = CtoF(SetVal) 

End Property 

Public Property Get DegreesCentigrade() As Single 

DegreesCentigrade = m_DegreesCentigrade 

End Property 

- A Property Let routine executes when the user changes the corresponding property. Note that each Property Let routine in this class sets not only the value that the object's user has changed, but the converted value as well. Thus, changing the property of DegreesCentigrade instantly changes both m_DegreesCentigrade and m_DegreesFahrenheit and vice versa. The Property Get routines do just the opposite. These execute when an object's property is read. In this class, these routines simply return the value of the internal variable corresponding to that property.

- To use the class, declare a variable of the class and set it using the new operator. Note! Whenever you use the Set operator to create an instance of an object, you should be sure to set the object to Nothing when you are done using it. Not doing this will cause memory leaks. It is a good idea to get into the habit of adding the statement that sets the object to Nothing when you enter the set statement in the first place. That way you won't forget about it later on:

Dim tTemp As Temperature 'Declare a variable of type Temperature 

Set tTemp = New Temperature 'Create an instance of the Temperature class 
'The code to use the object will go here 
set tTemp = Nothing 

- The properties and methods of the class are accessed via the familiar dot notation. For example:

tTemp.DegreesFahrenheit = 32 
debug.print tTemp.DegreesCentigrade 

- Let's put all the pieces together into a project. The entire listing for the class module and test program follow. The code to paste into Form 1 and the class module are indicated. Be sure to paste the code that begins 'Paste this code into Form1' into Form1 and the code that begins 'Paste this into the Class Module - Temperature' - into the Class Module you created. (Be sure you did rename the class from Class1 to Temperature!)

' ------------- Paste this code into Form1 ------------- 

' This sample code is presented as is. 
' Although every reasonable effort has been 
' made to insure the correctness of the example 
' below, Idianna Software Inc. makes no warranty 
' of any kind with regard to this program sample 
' either implicitly or explicitly. 

' This program example may be freely distributed for the 
' use of writing computer programs only. Any other use of 
' this material requires written permission from Idianna Software Inc. 
' (c) 2000 Idianna Software inc. All rights reserved. 

' Title: Temperature Conversion Program 
' Purpose: Demonstrate how to build a simple class 
' Platform: Visual Basic - 32 bit 
' Author: Jon Vote 
' Contact: jon@idioma-software.com 
' Date: 04/00 
' 

Private Sub Command1_Click() 

Dim tTemp As Temperature 

Dim sTemp As String 

Set tTemp = New Temperature 

sTemp = Trim$(Text1.Text) 
If Not IsNumeric(sTemp) Then 
MsgBox "Please enter a valid number." 
Else 
If Option1(0).Value Then 'Fahrenheit to Centigrade 
tTemp.DegreesFahrenheit = CSng(sTemp) 
Label1.Caption = tTemp.DegreesFahrenheit & " degrees Fahrenheit is "
& tTemp.DegreesCentigrade & " degrees Centigrade." 
Else 
tTemp.DegreesCentigrade = CSng(sTemp) 
Label1.Caption = tTemp.DegreesCentigrade & " degrees Centigrade is "
& tTemp.DegreesFahrenheit & " Fahrenheit " 
End If 
End If 

Text1.SetFocus 

Set tTemp = Nothing 

End Sub 


Private Sub Text1_GotFocus() 

Text1.SelStart = 0 
Text1.SelLength = Len(Text1.Text) 

End Sub 


' ------------- End code for Form1 ------------- 


' ------------- Paste this into the Class Module - Temperature ------------- 

' This sample code is presented as is. 
' Although every reasonable effort has been 
' made to insure the correctness of the example 
' below, Idianna Software Inc. makes no warranty 
' of any kind with regard to this program sample 
' either implicitly or explicitly. 

' This program example may be freely distributed for the 
' use of writing computer programs only. Any other use of 
' this material requires written permission from Idianna Software Inc. 
' (c) 2000 Idianna Software inc. All rights reserved. 

' Title: Temperature Conversion Program 
' Purpose: Demonstrate how to build a simple class 
' Platform: Visual Basic - 32 bit 
' Author: Jon Vote 
' Contact: jon@idioma-software.com 
' Date: 04/00 
' 

Private m_DegreesFahrenheit As Single 
Private m_DegreesCentigrade As Single 

Public Property Let DegreesFahrenheit(ByVal SetVal As Single) 

m_DegreesFahrenheit = SetVal 
m_DegreesCentigrade = FtoC(SetVal) 

End Property 
Public Property Get DegreesFahrenheit() As Single 

DegreesFahrenheit = m_DegreesFahrenheit 

End Property 
Public Property Let DegreesCentigrade(ByVal SetVal As Single) 

m_DegreesCentigrade = SetVal 
m_DegreesFahrenheit = CtoF(SetVal) 

End Property 
Public Property Get DegreesCentigrade() As Single 

DegreesCentigrade = m_DegreesCentigrade 

End Property 
Private Function CtoF(C As Single) As Single 

'Convert Fahrenheit to Centigrade 

CtoF = C * 1.8 + 32 

End Function 
Private Function FtoC(F As Single) As Single 

'Convert Centigrade to Fahrenheit 

FtoC = (F - 32) / 1.8 

End Function 

' ------------- End code for the Class Module - Temperature ------------- 

(c) 2000 Idianna Software Inc.
(c) 2000 Jon Vote
All rights reserved


 
 
 
 
 

Tiene el Poder del
Cafe de Guatemala