Page 336 - Microsoft Office Excel 2003 Programming Inside Out
P. 336

Microsoft Office Excel 2003 Programming Inside Out

                    Defining Events
                             Events can be very useful in a class, but you can’t assume that everyone that uses your class
                             will actually use the events. Therefore, if you decide to use events in your class, you need to
                             ensure that the class will continue to function if the user doesn’t respond to any of the events.
                             The Event statement is used to define an event. For all practical purposes, this is effectively a
                             subroutine statement minus the code. This definition is necessary because it identifies the
                             parameters that will be passed to the event. The event definition is used by the by the Visual
                             Basic compiler to ensure that the number of parameters and the type of the parameters
                             match the definition.

                             Note  Although you can specify nearly any type of parameter you can use in a subroutine,
                             events can’t have named arguments, optional parameters, or ParamArray arguments.
             Chapter 14
                             A sample event definition might look like this:

                             Event DiscountError (value As Currency, Msg As String)

                             Within the class, you would use a RaiseEvent statement to trigger an event in the user program.
                             Following the RaiseEvent statement name is the name of the event, followed by a list of values
                             that will be passed to the user program. For example, this statement passes two values back to
                             the calling program.

                             RaiseEvent DiscountError(discount, “Illegal discount amount. “)

                             To use events in an application, the WithEvents keyword must be included when the object is
                             defined. Without the WithEvents keyword, all events will be ignored. The following statement
                             shows how you would declare an object with events:

                             Dim WithEvents MyObject As GardenCompany

                    Defining Private Variables, Subroutines, and Functions

                             Although you need not mark your subroutines or functions as Private in a class, you should
                             note that without the Private keyword, any subroutine or function will default to Public. In
                             many situations, this might not be a serious problem, especially if you are the one person
                             using the class. However, if you plan to share your class with others, you might find them
                             relying on a routine where you accidentally omitted the Private keyword, which means you
                             can’t change the definition of the routine without impacting all the programs that use it.











                310
             Part 4:  Advanced VBA
   331   332   333   334   335   336   337   338   339   340   341