Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7403911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:10:45+00:00 2026-05-29T05:10:45+00:00

I have instances of a class MyClass , a and b . MyClass has

  • 0

I have instances of a class MyClass, a and b. MyClass has a field Text. I would like to assign an event MyEvent(MyClass target) to b.Selected, such that when fired, this event modifies a.Text. I have some code that looks like this:

void MainMethod() 
{
    var a = new MyClass();
    a.Text = "Foo";

    var b = new MyClass();
    b.Selected += MyEvent(a); // Make the event fire when b is "selected"
}

void MyEvent(MyClass target) {
    target.Text = "Bar";
}

To clarify, my intention is that a‘s Text will be “Foo”, but if b‘s Selected event fires, it a‘s Text will become “Bar”.

I tried to implement something similar to the above, but it did not work. (VS2010 claimed that MyEvent(a) is an event with a null argument, even though I don’t see why a is null.)

I don’t understand events very well (I’m trying to learn them) so sorry if this is obvious.

DETAILS:

In practice, I am using a LabelSelectable not MyClass. LabelSelectable extends Control, which is a class for displaying various things such as pictures, buttons and labels on a screen (this is XNA if relevant). Basically in this case, a bunch of these selectable labels can be cycled through with arrows, and if you press Enter when on a given label, its Selected event fires.

I was actually following this tutorial, and you can find the details of the implementation there. (I don’t mean to be unhelpful but I’m not sure exactly what parts are relevant and what are not, and if I put everything here, it would be a lot of code.)

As far as I can see, “MyClass” looks something like this:

MyClass 
{
    public event EventHandler Selected;

    public string Text
    {
        get { return text; }
        set { text = value; }
    }

    void OnSelected(EventArgs e)
    {
        if (Selected != null)
        {
            Selected(this, e);
        }
    }
}

I can get the equivalent of the below code to work, by the way:

void MainMethod() 
{
    var a = new MyClass();
    a.Text = "Foo";

    var b = new MyClass();
    b.Selected += MyEvent; // Make the event fire when b is "selected"
}

void MyEvent(object sender, EventArgs e) {
    Console.WriteLine("Event fired!");
}

It would output stuff to the console when b is Selected.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T05:10:46+00:00Added an answer on May 29, 2026 at 5:10 am

    You shouldn’t be placing parameters in your event handler declaration.

    b.Selected += MyEvent(a); // Make the event fire when b is "selected"

    should just be

    b.Selected += MyEvent; // Make the event fire when b is "selected"

    Either MyClass should be responsible for passing the value of target to the invocation of Selected or you must rely on the MyEvent method itself to determine which object to modify.

    It may be adventageous to follow a standard eventing pattern where the source of the event is passed in as a parameter to the event handler:

    void MyEvent(object sender, EventArgs e) {
        var source = sender as MyClass;
    
        var target = /* do something here to find the target based on the sender */ 
    
        target.Text = "Bar"; 
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In C++, since you can have class-instances as members in structs, the compiler has
I have 2 instances of a class that implements the IEnumerable interface. I would
I have a class MyClass. It has instance variables passedInVar1, passedInVar2, etc. whose values
If I have a class like this: class myClass { string foo; string bar;
I have a java class that looks like public class MyClass { private final
I have Parent class X, and Parent class H. H has in its field
let's say we have a c++ class like: class MyClass { void processArray( <an
I have a class defined like this: class MyClass { int x; public: MyClass(int
We have a lot of class code which has some boilerplate like the following:
I have a javascript class defined like this: myClass = { var1:0, var2:0, initVars:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.