Thread: about *Sender

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    61

    about *Sender

    Hi
    I am trying to understand when sender is useful.In what circumstances i need this?

    I have a sample code :
    Code:
    void __fastcall TForm1::Button1Click(TObect *Sender) 
    { 
    TButton *Button = dynamic_cast<TButton *>(Sender); 
    if (Button) 
        { 
        // do something if a button was pressed. 
        } 
    else 
        { 
        // do something when a call was made 
        // passing a NULL value for  *Sender) 
        } 
    }
    here it writes :
    // do something when a call was made
    // passing a NULL value for *Sender)
    How can it possible?I mean what type of code passing NULL value for *Sender.I am confused.

    Please help me to understand the functionality of sender.
    Thanks.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well dynamic_cast can also return NULL for a valid pointer, if it's not valid to convert whatever object is passed into a Button.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    While I'm not advocating doing this (there are better techniques), it is also possible to have one event handler call another. For example;
    Code:
    void __fastcall TForm1::Button2Click(TObect *Sender)
    {
         Button1Click((TObject *)NULL);
         Button1Click((TObject *)NULL);
    }
    If you do this, it can be useful for your event handler to be able to detect if it has been sent directly in response to a user event or has been triggered in some other way.

  4. #4
    Registered User
    Join Date
    Jan 2006
    Posts
    61
    ok.
    Thanks friends...

Popular pages Recent additions subscribe to a feed