Click here to Skip to main content
15,886,059 members
Home / Discussions / C#
   

C#

 
GeneralRe: curve from bezier cusps Pin
googlejumbo1-Jul-09 7:12
googlejumbo1-Jul-09 7:12 
GeneralRe: curve from bezier cusps Pin
molesworth1-Jul-09 11:42
molesworth1-Jul-09 11:42 
QuestionAnonymous methods problem Pin
Narvius30-Jun-09 10:07
Narvius30-Jun-09 10:07 
AnswerRe: Anonymous methods problem Pin
Christian Graus30-Jun-09 10:29
protectorChristian Graus30-Jun-09 10:29 
GeneralRe: Anonymous methods problem Pin
Narvius30-Jun-09 10:37
Narvius30-Jun-09 10:37 
GeneralRe: Anonymous methods problem Pin
Christian Graus30-Jun-09 13:29
protectorChristian Graus30-Jun-09 13:29 
GeneralRe: Anonymous methods problem Pin
Narvius30-Jun-09 13:48
Narvius30-Jun-09 13:48 
GeneralRe: Anonymous methods problem Pin
dojohansen1-Jul-09 0:27
dojohansen1-Jul-09 0:27 
Christian Graus wrote:
target.Character and target.passableBy will work, because the anonymous method has the scope of the spot it was created


How is that possible? In his code, target is just the loop variable and the anonymous method - the event handler - might run a year after the loop has finished. The handler will exist as long as delegates referring to it does, but the local variable "target" goes out of scope once the loop finishes, and it's value is changed on each loop iteration. The anonymous method is still a method and it seems to me it should have it's own scope. (I see that it makes sense for things where the anonymous method actually *executes* in the containing methods, like when using List<T>.Find(Predicate<T> match), but here we're attaching an event handler.)

I've seen many of your posts and have the impression you're in the know, so it would be nice if you can explain how this works with anonymous methods as event handlers. I'm also wondering if doing it like the OP did would lead to multiple anonymous methods? It seems it would have to be so if the method shares scope with it's containing method.

To return to the OP's issue, surely the normal way to do this is to use a single event handler, attach it to all the targets, and pass a target to the eventhandler as "sender" in the EventHandler delegate?

public class Thing
{
   public event EventHandler Touched;

   public void Touch()
   {
      //...
      if (Touched != null) Touched(this, EventArgs.Empty);
   }
}


public class OtherThing
{
   List<Thing> Things = App.GetThings(); // Assume we have things...
   public void Foo() 
   {
      foreach (Thing t in Things)
        t.Touched += new EventHandler(thing_touched);
   }

   static void thing_touched(object sender, EventArgs e)
   {
      Thing source = (Thing)sender;
      // ... work with the Thing ...
   }
}


(I believe it is a good practice to make all non-virtual methods that depend only on their parameters static. It works with an instance method as well, but there is no reason for it to be an instance method.)

I hope you will comment on this as I'm now confused about the scoping of anonymous methods.
GeneralRe: Anonymous methods problem Pin
Narvius1-Jul-09 4:20
Narvius1-Jul-09 4:20 
GeneralRe: Anonymous methods problem Pin
dojohansen1-Jul-09 4:50
dojohansen1-Jul-09 4:50 
GeneralRe: Anonymous methods problem [modified] Pin
Narvius1-Jul-09 6:32
Narvius1-Jul-09 6:32 
GeneralRe: Anonymous methods problem Pin
dojohansen1-Jul-09 21:05
dojohansen1-Jul-09 21:05 
GeneralRe: Anonymous methods problem Pin
Narvius2-Jul-09 0:06
Narvius2-Jul-09 0:06 
QuestionCopy project files to a new project Pin
Member 354381030-Jun-09 9:17
Member 354381030-Jun-09 9:17 
AnswerRe: Copy project files to a new project Pin
Narvius30-Jun-09 10:18
Narvius30-Jun-09 10:18 
GeneralRe: Copy project files to a new project Pin
dojohansen1-Jul-09 0:36
dojohansen1-Jul-09 0:36 
QuestionUninstalling (setup and d eployment project) - newbie question Pin
choo_chu30-Jun-09 9:02
choo_chu30-Jun-09 9:02 
AnswerRe: Uninstalling (setup and d eployment project) - newbie question Pin
EliottA30-Jun-09 9:19
EliottA30-Jun-09 9:19 
GeneralRe: Uninstalling (setup and d eployment project) - newbie question Pin
choo_chu30-Jun-09 9:52
choo_chu30-Jun-09 9:52 
AnswerRe: Uninstalling (setup and d eployment project) - newbie question Pin
Jimmanuel30-Jun-09 10:08
Jimmanuel30-Jun-09 10:08 
GeneralRe: Uninstalling (setup and d eployment project) - newbie question Pin
choo_chu30-Jun-09 10:45
choo_chu30-Jun-09 10:45 
GeneralRe: Uninstalling (setup and d eployment project) - newbie question Pin
choo_chu30-Jun-09 11:22
choo_chu30-Jun-09 11:22 
GeneralRe: Uninstalling (setup and d eployment project) - newbie question Pin
Jimmanuel30-Jun-09 11:27
Jimmanuel30-Jun-09 11:27 
Questionoverriding System.Diagnostics..::.DebuggerStepThroughAttribute Pin
Dan Neely30-Jun-09 8:45
Dan Neely30-Jun-09 8:45 
AnswerRe: overriding System.Diagnostics..::.DebuggerStepThroughAttribute Pin
Rob Smiley30-Jun-09 11:43
Rob Smiley30-Jun-09 11:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.