# EventDispatcher and Delegate syntax problem

**URL:** <https://forum.kirupa.com/t/eventdispatcher-and-delegate-syntax-problem/232931>\
**Category:** flash\
**Created:** [July 18, 2007, 8:21pm UTC](https://forum.kirupa.com/t/eventdispatcher-and-delegate-syntax-problem/232931 "2007-07-18T20:21:51Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![ironchefmoto](https://avatars.discourse-cdn.com/v4/letter/i/91b2a8/32.png) [@ironchefmoto](https://forum.kirupa.com/u/ironchefmoto)\
**Post date:** [July 18, 2007, 8:21pm UTC](https://forum.kirupa.com/t/eventdispatcher-and-delegate-syntax-problem/232931/1 "2007-07-18T20:21:51Z")

</div>

So – I found, from a co-worker, a novel way to handle a lot of EventDispatcher events from a class instance talking back to the main timeline, using the Delegate class. I’m not sure if it’s kosher, but it seems to work… **unless I goof up the syntax when I set it up.**

Here’s the code that works:

```auto
itemsDelegate = Delegate.create(this, itemsChange);
[COLOR=Red]**function itemsChange(evt_obj:Object):Void**[/COLOR]
{
    switch(evt_obj.type)
    {
        case "answer" :
            trace("Answer selected : " + evt_obj.answer);
            break;
        default :
            // do nothing for default case
            break;
    }
}

queItemClip.addEventListener("answer", itemsDelegate);

```

I can fire off, from the that class instance (queItemClip) as many event types as I need and use the switch case to handle them all. Just a variation of writing the event listeners, I guess.

**However, I can’t get it to work if I write the code block this way:**

```auto
itemsDelegate = Delegate.create(this, itemsChange);
[COLOR=Red]**this.itemsChange = function(evt_obj:Object):Void**[/COLOR]
{
    switch(evt_obj.type)
    {
        case "answer" :
            trace("Answer selected : " + evt_obj.answer);
            break;
        default :
            // do nothing for default case
            break;
    }
}

queItemClip.addEventListener("answer", itemsDelegate);

```

Why does the named function variant work and the anonymous function fail? Keep in mind, I’m writing the anonymous function variant differently than…

```auto
var itemsChange:Function = function(evt_obj:Object):Void {}

```

Is it a scope issue?

Thanks,  
IronChefMorimoto
