Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer
This is the forum to discuss the Wrox book Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer by Rod Stephens; ISBN: 9780470596906
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old August 8th, 2011, 02:57 PM
Authorized User
Points: 44, Level: 1
Points: 44, Level: 1 Points: 44, Level: 1 Points: 44, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2011
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default Lesson 23b Try It:Delegates

Need some help

Code:
 class BankAccount
    {
        // Define the OverdrawnEventArgs type.
        public class OverdrawnEventArgs
        {
            public decimal currentBalance, invalidBalance;
        }

        // Define the OverdrawnEventHandler delegate type.
        public delegate void OverdrawnEventHandler(object sender, OverdrawnEventArgs args);

        // Declare the Overdrawn event.
       public event OverdrawnEventHandler Overdrawn;

        // The account balance.
        public decimal Balance { get; set; }

        // Add money to the account.
        public void Credit(decimal amount)
        {
            Balance += amount;
        }

        // Remove money from the account.
        public void Debit(decimal amount)
        {
            // See if there is enough money.
            if (Balance < amount)
            {
                // Not enough money. Raise the Overdrawn event.
                if (Overdrawn != null)
                {
                    OverdrawnEventArgs args = new OverdrawnEventArgs();
                    args.currentBalance = Balance;
                    args.invalidBalance = Balance - amount;
                    Overdrawn(this, args);
                }
            }
            else
            {
                // There's enough money.
                Balance -= amount;
            }
        }
    }
}
I am having a lot of difficulty distinguishing between the actual event and the event handler in this class...
I get that the code "Overdrawn(this,args)" raises the event...but hwo does it do that? It looks like it passes the class to the event?..
  #2 (permalink)  
Old August 8th, 2011, 05:46 PM
Rod Stephens's Avatar
Wrox Author
Points: 3,166, Level: 23
Points: 3,166, Level: 23 Points: 3,166, Level: 23 Points: 3,166, Level: 23
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2006
Location: , , .
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts
Default

This is a confusing issue. Here's a recap of the process:

Think about what it would be like if you were not defining the BankAccount class. For example, if Microsoft provided it in the Toolbox. In that case, the class would define the Overdrawn event. Your program would create an event handler to catch that event and take action.

So the event is defined by the class, but the event handler is defined outside of it. (Actually it could catch its own events, too, but that would be fairly unusual and advanced so let's stick to the normal case.)

Inside its code, this class does four event-related things:

1. It defines the OverdrawnEventArgs class that is uses to pass data to the event handler. (That's the convention. If you only want to pass int or string data, you can skip this.)

2. It uses the "delegate" keyword to declare a delegate representing the kind of method that the event handler must be. It's basically a data type that happens to be a method instead of a number or whatever. In this case, it's a data type named OverdrawnEventHandler which is a method that takes "object sender, OverdrawnEventArgs args" as parameters and returns void (nothing).

3. It uses the "event" keyword to declare the event. In this example, it says there is a public event named "Overdrawn" that can be handled by a method of the type OverdrawnEventHandler.

4. Finally it should raise the event somewhere. The code looks like it calls the Overdrawn event. Really what it is doing is telling the system to look for any registered event handlers and invoke them, passing them the indicated parameters.

So to your questions:
I am having a lot of difficulty distinguishing between the actual event and the event handler in this class...
The class defines the type of the event handler but doesn't actually contain one. The main program contains event handlers to catch the class's events.
I get that the code "Overdrawn(this,args)" raises the event...
Exactly.
but hwo does it do that? It looks like it passes the class to the event?..
The syntax looks like that but it really is telling the system, "Go find all registered event handlers and call them, passing them these parameters."

It might have been a little clearer if Microsoft had chosen a different syntax that made the difference more obvious between calling a method and firing an event. (In Visual Basic, you use the RaiseEvent keyword so it's pretty obvious.)

I hope that helps. Let me know if you need more information or if I've missed the point of your question.
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)
  #3 (permalink)  
Old August 8th, 2011, 06:54 PM
Authorized User
Points: 44, Level: 1
Points: 44, Level: 1 Points: 44, Level: 1 Points: 44, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2011
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Alright. Thanks for clarifying, Rod.
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Lesson 5 exercises. thelemur BOOK: Java Programming 24-Hour Trainer by Yakov Fain 9 March 29th, 2013 11:56 PM
Lesson 10 sergeySE BOOK: Java Programming 24-Hour Trainer by Yakov Fain 1 July 3rd, 2011 06:20 PM
Lesson 8 sergeySE BOOK: Java Programming 24-Hour Trainer by Yakov Fain 1 June 29th, 2011 09:49 AM
Lesson 6 Try It zavodney BOOK: Stephens' Visual Basic Programming 24-Hour Trainer 2 June 6th, 2011 10:07 PM
Lesson 3 question soneal777 BOOK: Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer 1 August 30th, 2010 06:17 PM



All times are GMT -4. The time now is 09:33 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.