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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T09:17:44+00:00 2026-06-09T09:17:44+00:00

I have two classes, Input, and EventSystem. EventSystem is an internal (non-system dependent) class

  • 0

I have two classes, Input, and “EventSystem”. EventSystem is an internal (non-system dependent) class that handles ‘internal application events’. Input is a dependent class (system dependent) and handles key/button events and maps them both to key events and “EventSystem”. I will show you how I’m currently passing data through, it looks very clean on the inside, but very dirty on the outside. Does anyone know of a better way–or easier method–to passing down custom values?

EventSystem:

// Raisable Events
public enum EventType { Action, Cancel };

// Base for custom arguments
public class EventArguments
    {
        public double time;

        public EventArguments(double _time)
        {
            time = _time;
        }
    }


// Event Delegate (invoked on raise)
public delegate void Event(EventArguments eventArgs);

// full class
static class EventSystem
{
    private static Dictionary<EventType, Event> eventMapping = new Dictionary<EventType, Event>();

    public static void HookEvent(EventType eventType, Event _event)
    {
        if (eventMapping.ContainsKey(eventType))
        {
            eventMapping[eventType] += _event;
        }
        else
        {
            eventMapping.Add(eventType, _event);
        }
    }

    public static void RaiseEvent(EventType eventType, EventArguments args)
    {
        if (eventMapping.ContainsKey(eventType))
        {
            eventMapping[eventType].Invoke(args);
        }
        else
        {
            // do nothing
        }
    }
}

My input args simply inherit EventArguments.

// Inherits EventArguments (double time) and adds it's own, "bool pressed"
class KeyInputArguments : EventArguments
{
    public bool pressed;

    public KeyInputArguments(double time, bool _pressed) :
        base(time)
    {
        pressed = _pressed;
    }
}

When a key is pressed it fires key (input) events, then it checks to see if the key is mapped to an internal event and raises it. A separate class (Config) handles all the configuration of mapping/binding Keys to events.

// Raise on press
EventSystem.RaiseEvent(eventType, new KeyInputArguments(time, true));
// [...]
// Raise on release
EventSystem.RaiseEvent(eventType, new KeyInputArguments(time, false));

Finally, in order to fire the event, we have to register the key to the event (this is the ‘external‘ code)

// Hook our "Enter" function into the Action event
EventSystem.HookEvent(EventType.Action, Enter);

// [...]

public void Enter(EventArguments eventArg)
{
    if (((KeyInputArguments)eventArg).pressed == false)
    {
        Error.Break();
    }
}

Everything is great until I see ‘(((‘. This is an ugly result of my limited knowledge of C# and OOP programming in general.

I cannot change the Enter method argument because Event delegate explicitly requires EventArguments. (Even though KeyInputArguments inherits it ?). I also don’t understand why it takes so much to cast eventArg to KeyInputArguments.

Finally, I also tried this (though I don’t like it that much)

KeyInputArguments keyInputArguments = (KeyInputArguments)eventArg;
            if (keyInputArguments.pressed == false)

The reason I require custom data is that I plan on receiving input from multiple forms of input, such as gamepads. This means I can then process system dependent data (gamepad device information) into independent arguments. This limits system dependent data to my Input class, while leveraging my event system internally as independent. Is there a better method for what I’m doing?

  • 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-09T09:17:45+00:00Added an answer on June 9, 2026 at 9:17 am

    Put this code in your project somewhere:

    public static class SomeClassName
    {
        public static T CastTo<T>(this object source)
        {
            return (T)source;
        }
    }
    

    This enables you to write

    if(!eventArg.CastTo<KeyInputArguments>().pressed)
        Error.Break();
    

    I really like this because it conserves the left to right order of the things. This is especially good when writing linq.

    In answer to your question “Even though KeyInputArguments inherits it ?”. The problem here is that you have defined a general purpose method that uses a base class, it can’t know about inherited classes. You would need to use generics on that method to fix that issue, eg passing in the event type as a generic parameter

    public static void HookEvent<TEventType>(....)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two classes Foo and Bar that Bar extends Foo as below: class
I have two classes, one that inherits from the other. The base class is
I have two classes (MVC view model) which inherits from one abstract base class.
I have two classes set up as follows: class Point { protected: double coords[3];
I have two classes, class A and class B. Class A has a mission
I'm writing an application which has two classes that provide basically the same functionality
I have defined two classes below: public class junk { private BigInteger a =
I have a question, now i have two classes as following public class mainClass
I have two sets of of input fields like this, differentiated by their classes:
i have two class, i want getting with attr first class in input. how

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.