Creating a Class Interface in an ActiveX DLL

The chair object also has some attributes, such as color, height, and whether or not wheels are attached. These attributes can be added to the class as properties. Here’s how to enhance the class clsChair by adding a property for color:

  1. Choose Tools | Add Procedure.

  2. Type the name of the property in the Name text box, as shown in Figure 13-5.

  3. Choose Property in the Type area.

  4. Choose Public in the Scope area.

  5. Click OK and code should appear in the code window, showing framework code for Let and Get properties.


    Figure 13-5: Add Procedure dialog box, with Color property

Object Browser

A property is a special pair of functions that are designed to emulate a single attribute for the class when other software is consuming it. One function is used for reading the value and the other function is used for writing the value. Press the F2 button to open a special viewer called the Object Browser, which shows the interface to all of the software that is being used in the project open in the VB IDE. In Figure 13-6, the ConfigSeat library is selected in the top-left combo box, and the class clsChair is listed in the Classes list on the left side of the window.

click to expand
Figure 13-6: Object Browser showing the clsChair class and color property

Select the clsChair class to reveal the newly added property named color in the right pane. Select the color property, and the summary area at the bottom of the window describes the property in detail, identifying the type of parameter that color must be and the class and library to which it belongs.

The color property as it currently exists in the clsChair class is pretty worthless, because no variables have been set to hold the value of the property. Without variables, setting a value for the property or getting a value from it will result in nothing being stored or acquired from the property. Because the property is used to change a value in the class that will exist in memory while the class is instantiated, a local variable must be declared that has scope over the entire class.

The color values are strings that describe the color of the chair, so a private string variable named m_sColor will be declared at the top of the class listing. This variable will hold the value of the chair’s color. The keyword private makes the variable inaccessible by software outside of the class itself. The scope private is useful to control how consumers access class members, properties, subroutines, and functions. The class may need to change other values or validate the color when it is set, and if the local variable m_sColor were public, the consumer of the class could change the value of m_sColor and the class would not know it.

It is a good practice to always use a property to allow an attribute in a class to be set or read. In the short term, it adds an additional burden to the programmer to code it and may also increase the complexity of the solution, but doing this will pay off by making changing the class less difficult in the future. If the business rules for the class were to change in the future, updates to the COM object would be much easier behind the COM public interface because code hidden behind the property functions could be used to alter the meaning of the class attribute without changing the public interface to the class. Changing the interface of the class requires the consumers of the class to be changed also, so the long-term burden for not maintaining this abstraction between the class’s internal members and the class’s external interface can be greater than the initial burden.

Listing 13-1 shows the code that creates the class clsChair.

Listing 13-1: clsChair With Support to Read and Write the Color to the Instance Option Explicit

start example
 '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ '   constants to class Private Const DEFAULT_COLOR = "Brown" '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ '   locals to class 'holds the color of the chair Private m_sColor As String '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Public Property Get color() As Variant     color = m_sColor End Property Public Property Let color(ByVal vNewValue As Variant)     m_sColor = vNewValue End Property Private Sub Class_Initialize()     'set default color on instantiation     m_sColor = DEFAULT_COLOR End Sub
end example

It is a good practice in VB to place the statement Option Explicit at the top of each code file. This statement causes VB to check for undeclared variables being used in the code at compile time. If any undeclared variables are found, VB will fail to complete the compile and provide an error message. This statement helps the programmer by enforcing the use of declared variables so that if a variable was misspelled, for example, VB would catch it—the misspelled version would likely not be declared. You might spend hours attempting to figure out why a variable does not contain a value when it should, only to discover that the variable that was assigned the value was misspelled in a critical line of code. Tracking these types of bugs is difficult, so the Option Explicit command is useful at eliminating these types of situations. You can set VB to add this statement automatically to the top of every code file by choosing Tools | Options, opening the Editor tab, and checking the Require Variable Declaration checkbox.




IIS 6(c) The Complete Reference
IIS 6: The Complete Reference
ISBN: 0072224959
EAN: 2147483647
Year: 2005
Pages: 193

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net