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

  • Home
  • SEARCH
  • 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 7408731
In Process

The Archive Base Latest Questions

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

I have two lists, one with events, one with Actions(functions) The reason for the

  • 0

I have two lists, one with events, one with Actions(functions)

The reason for the lists is to allow a user to connect the two together through an interface. So I am trying to create a method which takes the event and has the action added to it.

I want to be able to do TheEvent += TheAction, and then be able to remove it later with -=.

I am sorry if how to do this is obvious, I couldn’t come up with the right keywords to search for, “connect action to event” is the best I thought of and it is pretty useless in a search.

Nothing I have come up with will allow me to add an action to an event, but perhaps I am going about it wrong. Any advice on the best way to allow a user to select an event and a function to trigger from the event would be appreciated.

  • 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:57:56+00:00Added an answer on May 29, 2026 at 5:57 am

    Events are immutable. If you add or remove an event handler, a new event (or multicast delegate) is created. This also means, that you must be careful when adding events to a list. This will not work

    public event EventHandler Event0;
    public event EventHandler Event1;
    public event EventHandler Event2;
    
    public List<EventHandler> Events;
    
    public void AddEvents( )
    {
        Events = new List<EventHandler> { Event0, Event1, Event2 };
        Event0 += new EventHandler(EventHandler0);
        Events[1] += new EventHandler(EventHandler1);
    }
    

    If you add an event handler to Event0 the corresponding list entry will still reference the old Event0! If you add an event handler to Events[1], Event1 will not be updated!


    I do not know if this is what you mean, however, if you have these two lists

    public List<EventHandler> Events;
    public List<Action<object, EventArgs>> Actions;
    

    You can add en event hander (an action) to an event like this:

    Events[i] += new EventHandler(Actions[j]);
    

    and raise an event like this

    EventHandler eh = Events[i];
    if (eh != null) {
        eh(this, EventArgs.Empty);
    }
    

    EDIT (in response to your comment):

    Here again, you must act on the original event list and not on an event passed as parameter, because of this immutable problem explained above. If necessary, you could also pass the list itself as an additional parameter.

    public void AddActionToEvent(Action<object, EventArgs> action, int eventIndex)
    {
        Events[eventIndex] += new EventHandler(action);
    }
    

    For your other problem of how to let the user select events and actions, I would suggest of either having four lists, where as the events and action lists would be paralleled with two corresponding string lists with the names of the events and actions. Alternatively, you could wrap the events and actions in a class containing them and their names

    public class EventWrapper
    {
        public string Name { get; set; }
        public event EventHandler Event;
    
        public override string ToString()
        {
            return Name;
        }
    
        public void RaiseEvent(object sender)
        {
            EventHandler eh = Event;
            if (eh != null) {
                eh(sender, EventArgs.Empty);
            }
        }
    }
    
    public class ActionWrapper
    {
        public string Name { get; set; }
        public Action<object, EventArgs> Action { get; set; }
    
        public override string ToString()
        {
            return Name;
        }
    }
    

    Then you would define the lists like this

    public List<EventWrapper> EventsWithNames;
    public List<ActionWrapper> ActionsWithNames;
    

    Now you can use these lists directly as data source in combo boxes or list boxes. The overridden ToString methods of the wrappers automatically provide a string to display.

    The add method can now be rewritten as

    public void AddActionToEvent(ActionWrapper actionWrapper, EventWrapper eventWrapper)
    {
        eventWrapper.Event += new EventHandler(actionWrapper.Action);
    }
    

    The wrappers also defuse the immutable situation, since we do not pass the event as parameter but its wrapper.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two views (for simplicity sake), one called Events, one called Friends. Their
I have two sortable lists, one being nested, with a mouse over effect on
I have two tables. One of events and one of articles. Each event has
I am using this method http://jqueryui.com/demos/sortable/#connect-lists to connect two lists that i have. I
I have two lists. At start only the first one has visible elements, the
I have two entities User and Event. Each user has a list of events
I have two entities one is Event and another is User. User can like
I have two drop down lists. Second one is populated based on value chosen
I have two lists. The first one is hard-coded and the contents never change.
I have two list on my request on jsp. First one is productGroupName, and

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.