about *Sender

This is a discussion on about *Sender within the C++ Programming forums, part of the General Programming Boards category; Hi I am trying to understand when sender is useful.In what circumstances i need this? I have a sample code ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,208
    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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21