Troubleshooting this TutorialSometimes, even though you try hard to understand the information in a tutorial, things don't work out quite like you want it to. This page is here to help you resolve any problems you might be having with the tutorial on events in C#. The Common Mistakes section describes common problems that people have when doing the things in this tutorial, and how to resolve them. The Frequently Asked Questions section describes questions that people have that aren't related to mistakes, but rather, trying to understand the stuff better or exploring how it can be used. If your problem or concern isn't addressed here, feel free to add a comment below, so that I know where you're having trouble. I like to keep these pages fairly clean, so I may remove comments that I felt like have been addressed. If I remove your comment and you don't feel like the problem has been fixed, repost the question and we'll take another look at it. If a tutorial has a mistake in it, I will fix the mistake and reply to the comment with a brief explanation. However, after a couple of weeks I'll likely go back and remove the original comment as well as my reply, because, hopefully, the problem will have been fixed, and it won't be a concern any more. |
Common MistakesNone listed yet… |
Frequently Asked QuestionsNone listed yet… |
In the tutorial on Events, the section regarding attaching HandlePointChanged to the event PointChanged does not work. [error CS0120: An object reference is required for the non-static field, method, or property 'test_csharp.main.HandlePointChanged(object, System.EventArgs)']
This is an interesting little problem. If it's what I think it is, it has nothing to do with events, per se. (Well, it kind of does, because you're attaching a method to an event, but the fundamental problem isn't tied to the event itself.)
I think the problem is that you're probably in the static Main method, trying to attach a non-static HandlePointChanged method to the event. The problem you're running into is the same problem that would prevent you from directly calling the non-static HandlePointChanged method from Main. You need an instance of the method to call an instance (a.k.a. "non-static") method.
In this case, one solution might be to make your HandlePointChanged method be static as well.
Your other choice is, of course, to leave the method non-static and create an instance of whatever class it's in to do the attaching. From the Main method, this means creating a new instance of the default Program class (i.e., new Program().HandlePointChanged) but in this particular case, it sounds kind of dumb to create an instance of the Program class like this.
Hmm… this last paragraph is a little on the confusing side. The point is, you can attach it to instance methods, you just need an instance of the type to attach things. Making instances of the Program class seems kind of silly here, but you may have other types that it makes sense for.
Considering the wording that is in the tutorial right now, I can see why this might be causing some problems. I should probably go clean that part up a little bit.
Post preview:
Close preview