The AX5tat Class Custom Members VB Help

Public Property Get Average() As Double
Dim dSum As Double
Dim itm As Long
For itm – 1 To Data Collection Count
dSum – dSum + DataC~llection(itm?
Average – dSum / Data Collection.Count
‘End Property
Public Property Get Min() As Double
Dim itm’As long
Min – lE+202.
For itm – 1 ,To Data Collection.Count
If Data Collection (itm) < Min Then _
Min – Data Collection(itm)
End Property
End Property
Public Property Get Max() As Double
Dim itm As Long ,
Max – -lE+202
For itm – 1 To Data Collection.Count
lf Data Collection (itm) > Max Jhen _
Max – Data Collection

The code reads-the data values stored in the Data Collection collection and calculates their basic statistics. You can implement Property Get procedures to calculate more advanced statistics than the ones shown here. Notice that the properties are read-only (it doesn’t make sense to set their values) and there’s no equivalent Property Let procedure. The code that implements the standard collection methods is identical to the code presented earlier in the section “Implementing a Collection.” Just change the name of the Class in the lines that raise run time errors.

Testing the AXStat Class

The test project for the AXStat Class is shown in Figure 15.7. The Create Data Set button adds 100 random values to the Class’ collection; The Display Data button retrieves the data values from the Class and displays them on the List Box control. Finally, the Show Statistics button calls the Class’ methods to calculate the basic statistics of the data set and display them. The buttons on the right demonstrate how to handle errors raised by the Class (we’ll get
to them later).

The AXStat Class test Form

FIGURE 15.7:

FIGURE 15.7:

To use the AXStat Class in a project you must first add a reference to ‘the Class to the project and then create an object variable that references the Class. The following declaration appears at the top of test Form’s Code window:

Dim·STATS As New Statistics

The New key word tells Visual Basic to create a new instance of the Statistics Class Module. The first Command button uses the STATS variable’s Add method to create a data set of 100 random values.

Populating the STATS Object with Random Data

Private Sub Command1-Click()
For ; – 1 To 100
STATS.Add Rnd() • 1000
Command2.Enabled – True
ComIand3. Enabled  True
End Sub

The Display Data button retrieves these values from the STATS variable with its Item method. The data values are retrieved with a loop that goes from 1 to STATS.Count.

Reading the Data Values from the STATS Object

Private Sub Command2_Cl1ck()
01. 1 As Long
For 1 = 1 To STATS.Count
List.Add ltem Fomat(STATS.Item(i). “000.000000”)
End Sub

The Show Statistics button calls the variable’s Average Mini and Max methods to calculate the basic statistics of the data set The code behind the Show Statistics button is shown next.

Using the AXSblt Class to Calculate Statistics

Private Sub Command3_Click()
Stats Msg = ‘There are ‘ & STATS.Count &_
1 points in the data set” & vbCrLf
StatsMsg = StatsMsg & “Their average is 1 & _
STATS.Average & vbCrLf =
StatsWsg  StatsWlg & ‘The smallest value 1 &_
STATS.Win & vbCrLf
StatsWsg = StatsMsg & “The largest value 1 & STATS.Max
WsgBox StatsWsg
end Sub

Handling Classes Errors

T..he AXStat Class contains code that raises errors from within the Class. As far as the application is concerned, these errors are identical to the ones generated by Visual Basic and they can be trapped. If the User attempts to access a non-existent

item (an item whose mdex is larger than the Count property) the Class will raise an error. The Class could handle the error by displaying an error message but this isn’t the way we handle errors that occur in Classes. We simply .raise a custom error and let the host application handle it

To generate a trappable run time error from within a Class (or ActiveX control . as you will see in the following chapter) you must use the Raise method of the Error object. The error number can be any Integer value, but you should make sure it doesn’t coin  with a Visual Basic error number. The range 0 to 512 is reserved for Visual Basic-and you should never use error numbers in this range. you can use any other error number, but it’s recommended that you use small integers and add the vb Error Object constant to them. For example,u your–class can detect . 8 types of errors, you can assign to them the values 1 through 8 and add the-con- “Error Object. Should another control use the same error numbers you can find out which control generated the error by examining the Err object’s Source property. To find out the original error number, the application can subtract the same constant from the Err.Number value.

Another interesting property of the “‘vb Error project constant is that its value is a very large negative number (-2147421504). Thus, all error numbers returned by custom Classes will be negative and you-tan-easily distinguish them from regular Visual Basic errors in your code

The two buttons on the right side of the test Form generate run time errors by invoking the members of the AXStat Class with invalid arguments. The next listing shows how they call the Item and Remove methods and the corresponding error-trapping code.

The error number and the error’s source are set from within the Class’ code with Raise method of the Err object as previously discussed. To find the actual ‘error number raised by the Class subtract the constant vb Error Object from the Err.Number value. Even better you can declare a few constants in your code that correspond to the error values: Here’s an enumerated type with the errors raised by the AXStats Class:

public Enum AXStatErrors
‘AddError – 1
RemoveError – 2
End Enum

The constants for the error values will appear in the Object Browser under the Class AXStats Errors and you can use them from within your application’s code _with the expressions AXStat Errors .Add Error and AXStat Errors. Remove Error.

Start the application and click one of the two buttons on the right side of the Form. Both buttons cause a run time error by attempting to access the i m with the index 9999. The application will stop with an error message. Why didn’t the test project catch the run time error with its own error-handling routines.

The answer is that by default Visual Basic breaks at the location of the error while in design mode. This w y you can see the line that is causing the error and debug the appropriate procedure in the Class Module. If you create an executable file and run it outside the Visual Basic IDE the run time error will be trapped by the host application. You can also change the default behavior of Visual Basic’s error-trapping mechanism at design time. Open the Tools menu and select Options. In ‘the Options dialog box that will appear on the screen select the General tab as shown in Figure 15.8.

In the Error Trapping section of the General tab, the option Break in Class Module is selected by default. That’s why Visual Basic breaks as soon as the error occurs and the test project doesn’t get a chance to handle it with it Sown error handler. Select the Break on un handled Errors option (as shown in Figure 15.8) close the  options dialog box and run the test .project again. Click the Remove Error or Item Error button and this time the project will not end with a run time error. Instead a dialog box with an error message like’ the one shown in Figure 15.9 will be displayed .

Changing the default
behavior of the Error
Trapping mechanism
at design time

FIGURE 15.8:

FIGURE 15.8:

Trapping run time errors
generated by the AXStats


FIGURE 15.9:

FIGURE 15.9:


Posted on November 5, 2015 in Building AdiveX Components

Share the Story

Back to Top
Share This