Podstrony
- Strona startowa
- (ebook pdf) Teach Yourself SQL in 21 Days
- Linux. .Mandrake.10.Podręcznik.Użytkownika.[eBook.PL] (3)
- (eBook) James, William The Principles of Psychology Vol. II
- (Business Ebook) 101 Ebay Auction Secrets (1)
- Lem Stanislaw Dzienniki gwiazdowe t.1
- Cole Courtney Beautifully Broken. Tom 3. Zanim miłoÂść nas połączy
- [Borys Storch] Podstawy Obróbki skrawaniem
- Jacek Dabala Najwieksza przyjemnosc swiata
- Andrzej Sapkowski Narrenturm (3)
- Uczeń.Jedi.05.Jude.Watson Obrońcy.umarłych
- zanotowane.pl
- doc.pisz.pl
- pdf.pisz.pl
- akte20.pev.pl
[ Pobierz całość w formacie PDF ]
.If there are new lab results, you could raise an event.Your code would looksimilar to this:Dim miPatientID As IntegerPublic Property PatientID() As IntegerGetReturn miPatientIDEnd GetSetmiPatientID = Value check labs database for this patient if there are new lab resultsRaiseEvent LabResult( CBC )End SetEnd PropertyIf this were a real procedure, you wouldn t hard-code CBC into the event, but wouldinstead pull the lab type from the database.05 2203-x CH04 5/1/01 12:52 PM Page 83Creati ng Your Fi rst Cl ass Li brary 83Handling the Event in the ClientYou have two options for handling this event in the client.The first way to handle theevents is using the WithEvents keyword.The second way is to use the AddHandlerstatement.To use the WithEvents keyword, you need to declare the object and includeWithEvents.Notice that if you are using the WithEvents keyword, the declarationcannot be local to a sub or function.Therefore, this code will go outside any sub orfunction, at the module level in your client:Dim WithEvents Patient As PatientThis line assumes that you have imported the HealthCare namespace.Notice that thekeyword New is not in the preceding statement.You cannot use the WithEvents key-word in a declaration inside a procedure, nor can you use the New keyword in a decla-ration outside a procedure to create an object that has events.Therefore, you have todeclare the object using the WithEvents keyword outside of any procedure, and thenyou have to use the New keyword inside a procedure, as shown here:Dim WithEvents Patient As Patient.Public Sub Foo()Patient = New cPatient().End SubNow, to add an event procedure, click on the Class Name drop-down list box at thetop of the code window and choose Patient.In the Method Name drop-down listbox, choose LabResult.This creates an event procedure named Patient_LabResultthat looks like this:Public Sub Patient_LabResult(ByVal LabType As System.String) _Handles Patient.LabResult.End SubThe second way to handle events is to use the AddHandler statement.You now do nothave to define the class using the WithEvents keyword.Instead, you define it as youdid before.You must also have a sub or function that will act as the event procedure.Next, you use the AddHandler statement to tie a particular event from the object tothe procedure you created to handle the event.For example, assume that you wanted to create an event handler sub calledLabHandler.This procedure would be defined as a standard sub.It would have totake as arguments any parameters defined in the LabResult event.In the examplehere, the event named LabResult passes along a lab parameter with a data type ofstring.Therefore, your procedure would have to accept a string as an argument.05 2203-x CH04 5/1/01 12:52 PM Page 8484 Chapter 4: Bui l di ng Cl asses and Assembl i es wi th VB.NETWhen you use AddHandler, you specify the name of the event in the Patient classyou want it to handle (LabResult), but you must also refer to the procedure; in thiscase, LabHandler.However, you don t refer directly to LabHandler; instead, yourefer to the address of LabHandler, using the AddressOf operator.Your code to setup event handling with the AddHandler statement would look like this:Protected Sub button1_Click(ByVal sender As Object, _ByVal e As System.EventArgs) Handles button1.ClickDim Patient As New Patient()AddHandler Patient.LabResult, AddressOf Me.LabHandler.End SubPrivate Sub LabHandler(ByVal LabType As String).End SubNotice that in the AddHandler, you refer to the object variable (Patient) and theevent name (LabResult) using the Object.Event syntax.The advantage of this approach is that you can have one procedure handling multipleevents, even if the events are from different components.You can also hook eventsinto objects that you create at runtime.For example, you might get a collection ofobjects passed to you, but still be able to handle their events using this procedure.The Final CodeThe following code doesn t actually do anything, and some earlier changes areundone in this code
[ Pobierz całość w formacie PDF ]