One-liner



  • I'm just going to leave this here for now...

    SelectMachine.TextChanged += new EventHandler(SelectMachine_Click);


  • Either it is interpreting a pointer as a string, or we have a conversation operator overload combined with an assignement overload.

    Either way, this is strange indeed. But I do suppose that a += overload for a collection does make sense if you want to add to it.

    If we assume that "Changed" means that it is a set of callbacks/listeners, we can make sense of this code line.



  • @henke37 said:

    Either it is interpreting a pointer as a string, or ...
     

    It's most likely C# / .NET. It's a delegate += event handler. It's kind of a built-in collection for callbacks that does the right thing when you do += or call it. So there is no magic as far as the syntax / statement construction goes... the wtf is propagating textchange event as a click event.



  •  It's not actually propagating the textchange as a click event, it's simply registering a poorly-named event handler -- e.g. the textchange event won't cause a click event to be queued or anything, which is something I've ALSO seen and is considerably worse than a badly-named event handler.

     I suspect the same handler is handling both the click and textchange events -- for example, for some kind of validation -- and they just kept the default name when they created the click event, rather than a better practice of renaming it to reflect that it's not JUST a click event handler.



  • So the click event gets called everytime someone presses a key in the textbox.

     



  • @lesliev said:

    So the click event gets called everytime someone presses a key in the textbox.

     

    I have to actually test this to work out what really happens but I'm a bit afraid my brains will explode when I do that. Or turn to mush.



  • It's just a misnamed event.

    When the object SelectMachine's text is changed SelectMachine_Click is run.  No clicking necessary.

    However if there is a seperate method for clicking and text changing and he's swapped them around, god help us all when someone clicks on something or changes text.



  • @henke37 said:

    Either it is interpreting a pointer as a string, or we have a conversation operator overload combined with an assignement overload.
    Nah, that's just C#, though if you're in love with the syntax, you can have it in C++ too:

    #include <stdlib.h>
    

    template <typename Argument>
    class Callback {
    public:
    virtual void operator() (Argument) = 0;
    virtual bool operator== (Callback<Argument> &) = 0;
    virtual ~Callback() {};
    };

    template <typename Argument>
    class FunctionPtr : public Callback<Argument> {
    private:
    void (*function)(Argument);
    public:
    FunctionPtr(void (*function)(Argument)) {
    this->function = function;
    }
    void operator() (Argument a) {
    (*function)(a);
    }
    bool operator== (Callback<Argument> &callback) {
    FunctionPtr<Argument> *c = dynamic_cast<FunctionPtr<Argument> *>(&callback);
    return c && c->function == function;
    }
    };

    template <class Class, typename Argument>
    class MemberPtr : public Callback<Argument> {
    private:
    Class *obj;
    void (Class::*member)(Argument);
    public:
    MemberPtr(Class *obj, void (Class::*member)(Argument)) {
    this->obj = obj;
    this->member = member;
    }
    void operator() (Argument a) {
    (obj->*member)(a);
    }
    bool operator== (Callback<Argument> &callback) {
    MemberPtr<Class, Argument> *c = dynamic_cast<MemberPtr<Class, Argument> *>(&callback);
    return c && c->obj == obj && c->member == member;
    }
    };

    template <class Argument>
    class CallbackList : public Callback<Argument> {
    private:
    struct Entry {
    Callback<Argument> *entry;
    Entry *next;
    };
    Entry head, *tail;
    void operator=(CallbackList &l) {};
    public:
    CallbackList() {tail = &head;}
    ~CallbackList() {
    Entry *cur = head.next;
    while(cur) {
    Entry *tmp = cur;
    cur = cur->next;
    delete tmp->entry;
    delete tmp;
    }
    }
    CallbackList &operator+= (Callback<Argument> *callback) {
    Entry *tmp = new Entry();
    tmp->entry = callback;
    tmp->next = NULL;
    tail->next = tmp;
    tail = tmp;
    return *this;
    }
    CallbackList &operator-= (Callback<Argument> *callback) {
    Entry *cur = head.next;
    Entry *prev = &head;
    for(;cur;prev=cur,cur=cur->next) {
    if(*cur->entry == *callback) {
    prev->next = cur->next;
    delete cur->entry;
    if(cur->entry != callback)
    delete callback;
    delete cur;
    break;
    }
    }
    return *this;
    }
    void operator() (Argument a) {
    Entry *cur = head.next;
    for(;cur;cur=cur->next) {
    (*cur->entry)(a);
    }
    }
    bool operator== (Callback<Argument> &callback) {
    return this == &callback;
    }
    };



  • @lesliev said:

    So the click event gets called everytime someone presses a key in the textbox.

     

     

     Not exactly.  A method whose name sounds like a click event handler is set to handle the text changed event.  But it won't actually create a new event in the application's event queue or anything; no WM_CLICK message goes flying behind the scenes (which is a much worse WTF and sadly not too uncommon).

    I think the most likely probability is that he assigned the same event handler to two different events (which isn't bad practice) and he didn't change the event handler name to reflect its actual function (which is).  I've seen that a lot -- not quite so much in the current form, but I've seen plenty of times where a single event handler can process the same event for multiple controls, and the name is something like textBox1_TextChanged when it actually is set to handle the TextChanged event for textBox1, textBox2, textBox3, etc.

     


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.