![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Advanced Visual Basic Techniques
Bit Masks in AppointmentBookAppointmentBook uses bit masks to track events using the model/view/controller (MVC) paradigm. MVC is described in detail in the following section, Using the MVC Paradigm, but its use of bit masks is described here. AppointmentBook uses bit masks to define certain events. For example, when the user clicks on a new date in the month area, the application selects that date. The program associates this event with an event mask defined by the following code: Public Const CAL_SelectedDateChanged = &H2 AppointmentBook keeps a list of views that may be interested in certain events. Each view is an object that provides a value giving the bitwise Or of the event codes that interest it. For instance, the object that manages the daily appointment area needs to redraw the appointments whenever the user selects a new date. It also needs to redraw the appointments if the user changes an appointments start or stop time. This object uses the following value as its mask of interesting events: CAL_SelectedDateChanged Or CAL_AppointmentChanged Values describing the events that are interesting to each view object are stored in the programs Events collection. References to the objects themselves are stored in the corresponding entries in the Views collection. The NotifyViews subroutine takes as a parameter an event mask that represents the event or events that have just occurred. It uses the bit mask techniques described in the previous section to identify the views that are interested in the events. For each interested view, it invokes the views Notify subroutine, passing it the event mask so the routine can tell what event just took place.
Private Sub NotifyViews(event_mask As Long)
Dim i As Integer
For i = 1 To Views.Count
Notify this view if necessary.
If event_mask And Events.Item(i) Then _
Views.Item(i).Notify event_mask
Next i
End Sub
Using the MVC ParadigmThe interactions among AppointmentBooks various user interface elements are quite complex. For example, when the user clicks the up arrow on the month areas scroll bar, the following actions take place:
This is one of AppointmentBooks more convoluted interactions, but many of the others are complicated enough to be quite confusing. These sorts of interactions can make program maintenance a nightmare. Suppose, for example, the program needed to display daily schedules using a new display method. Adding the new code would require removing all of the code that managed the display using the old method. Changes would be required wherever the program used the old code to update the appointment display. This occurs whenever the user performs one of the following actions:
Removing the older appointment display code would require changes in many different parts of the program. The model/view/controller (MVC) paradigm uses three different kinds of objects to make managing these sorts of interactions easier. As its name implies, the three kinds of objects MVC uses are called models, views, and controllers. Briefly, a model is an object that represents some sort of data, device, or some other thing around which the interactions are centered. A view is an object that provides some sort of display of a model. A controller is an object that manipulates a model. The following sections discuss the basic duties of these objects. They also briefly describe the specific duties of the model, view, and controller objects used by AppointmentBook. Some of these objects are described in greater detail a bit later. The duties of models, views, and controllers are tied tightly together so you may find it difficult to understand how each object works separately. You may need to know how a view works before you can understand models, but you also need to understand models before you can fully understand views. To understand all the details, you may need to read the following sections two or three times. ModelsA model has two duties: managing application data and notifying views when the data is modified. The first duty simplifies the ways in which other parts of the program modify the data. The second makes even the most complicated object interactions manageable. Managing Application Data It is the models duty to manage all aspects of the actual data. The model is the only object that is allowed to do anything to the data directly. Only the model can load, save, modify, or even view the data. Other objects that need to interact with the data must do so using the models public subroutines, functions, and property procedures. For example, in AppointmentBook one of the many pieces of information stored in the model is the currently selected date. The model class CalendarModel provides a property let procedure SelectedDate that allows other parts of the program to select a new date. Code outside the CalendarModel class simply sets the property procedures value to the desired date, and the model makes the actual changes to the data. The following code shows how the Select Today command in the Appointments menu makes the current day the selected date. Visual Basics Date function returns the current date. Because all of the data modification details are hidden within the models SelectedDate procedure, this code is extremely simple.
Private Sub mnuApptToday_Click()
TheCalendarModel.SelectedDate = Date
End Sub
Notifying Views The second duty of a model is to notify interested views when an event occurs. In addition to storing a representation of the data, the model must contain a list of view objects that are interested in various events that may occur. When one of the models public routines or functions changes the data, the model must inform interested views of the change. To make view management possible, the model should provide a RegisterView subroutine. This routine takes as parameters a reference to a view object and a bit mask indicating the events that interest the object. The model stores this information for later use.
|
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-1999 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.
|