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 8438371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:41:45+00:00 2026-06-10T07:41:45+00:00

I am working on a card game project in C#. In this game, there

  • 0

I am working on a card game project in C#. In this game, there are numerous events that are fired that the cards on the table need to subscribe to. However, I need the cards to dynamically subscribe and unsubscribe to the events so that the cards that are not currently active will not respond to the events being fired.

My solution for this is to use an EventManager parent class, with one child for each event. Each of these managers will have subscribe / unsubscribe methods that take an EventHandler as a parameter. When called, the methods will subscribe / unsubscribe the provided handlers from the event assigned to that manager.

The code below is a basic Console application that uses the same design pattern as my card game, and has the same issues. You can paste it directly into Visual Studio and it will run.

using System;
using System.Collections.Generic;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var example = new ExampleCard();
            example.Subscribe();
            example.Events.FireEventOne(null, new EventArgs());
            example.UnSubscribe();
            example.Events.FireEventOne(null, new EventArgs());
            Console.ReadLine();
        }
    }

    public class EventContainer
    {
        public delegate void EventOne(Object sender, EventArgs r);
        public event EventOne OnEventOne;

        public EventContainer()
        {
            OnEventOne += Default_Handler;
        }

        public void Default_Handler(object sender, EventArgs e)
        {

        }

        public void FireEventOne(object sender, EventArgs e)
        {
            OnEventOne(sender, e);
        }
    }

    public class EventManager
    {
        protected EventContainer Events;

        public EventManager(EventContainer events)
        {
            Events = events;
        }

        public virtual void Subscribe(EventHandler handler)
        {
        }

        public virtual void Unsubscribe(EventHandler handler)
        {
        }
    }

    public class EventOneManager : EventManager
    {
        public EventOneManager(EventContainer events)
            : base(events)
        {
        }

        public override void Subscribe(EventHandler handler)
        {
            Events.OnEventOne += new EventContainer.EventOne(handler);
        }

        public override void Unsubscribe(EventHandler handler)
        {
            Events.OnEventOne -= new EventContainer.EventOne(handler);
        }
    }

    public class ExampleCard
    {
        public EventContainer Events = new EventContainer();
        private readonly List<EventManager> _subscribedEvents = new List<EventManager>();

        public ExampleCard()
        {
            _subscribedEvents.Add(new EventOneManager(Events));
        }

        public void Example_OnEventOneFired(object sender, EventArgs e)
        {
            Console.WriteLine("Event One was fired.");
        }

        public void Subscribe()
        {
            _subscribedEvents[0].Subscribe(Example_OnEventOneFired);
        }

        public void UnSubscribe()
        {
            _subscribedEvents[0].Unsubscribe(Example_OnEventOneFired);
        }
    }
}

EventOneManager.Subscribe() seems to work, but EventOneManager.Unsubscribe() does not; after calling that method, the example card stays subscribed to the event and I can’t figure out why, or how to fix it.

One thing I tried was removing the ‘new EventContainer.EventOne’ call making it look like this:

public override void Unsubscribe(EventHandler handler)
            {
                Events.OnEventOne -= handler;
            }

But I get this error: Cannot implicitly convert type ‘System.EventHandler’ to ‘ConsoleApplication1.EventContainer.EventOne” Any suggestions?

Also I would like to be able to use a custom EventArgs class, but I can’t figure out how to pass in a generic handler which will work with any of the events I am using. This I can work around, but I would prefer to use custom EventArgs.

  • 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-06-10T07:41:47+00:00Added an answer on June 10, 2026 at 7:41 am

    You need to change the event to be of type EventHandler.
    You should not make your own delegate type.

    To pass additional data, use EventHandler<T>.

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

Sidebar

Related Questions

I'm working with an array that looks like this: cards = [#<Cardshark::Card:0x9200cc @rank=:ace, @suit=:coins>,
I'm working on a card game in C# for a project on my Intro
I am working on a little card-swapping world-travel game that I sort of envision
I'm working on a game that allows players to click on a card and
I am working on a card game, as part of which I need to
I am currently working on a project that will involve credit card swipes for
I'm working on a GUI for a card game and am using the ACM's
I'm working on an online turn based card game for the PC. It features
I am working on a card game iPhone app and have a question about
I am working on a card game and among other things have a UIView

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.