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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T20:41:28+00:00 2026-06-07T20:41:28+00:00

Why can’t I directly pass custom event arguments into a method subscribing to my

  • 0

Why can’t I directly pass custom event arguments into a method subscribing to my event even though my custom event args class directly inherits from EventArgs?

For example, consider the two below classes. One is the class I want to work with, the other inherits from EventArgs, and contains some additional information related to the event:

using System;

namespace ConsoleApplication11
{
    class MyClass
    {
        public event EventHandler MyEvent;

        public void MyMethod(int myNumber)
        {
            Console.WriteLine(myNumber);

            if(myNumber == 7)
            {
                MyEvent.Invoke(null, new MyCustomEvent() { Foo = "Bar" });
            }
        }
    }

    class MyCustomEvent : EventArgs
    {
        public string Foo { get; set; }
    }
}

So if a number 7 is passed into MyMethod, I want to invoke MyEvent passing in the a new instance of the MyCustomEvent class to any method subscribing to my event.

I subscribe to this from my main program like so:

using System;

namespace ConsoleApplication11
{
    class Program
    {
        static void Main(string[] args)
        {
            MyClass myClass = new MyClass();
            myClass.MyEvent += new EventHandler(myClass_MyEvent);

            for (int i = 0; i < 9; i++)
            {
                myClass.MyMethod(i);
            }
        }

        static void myClass_MyEvent(object sender, EventArgs e)
        {
            //Do Stuff
        }
    }
}

Even though I am passing in a MyCustomEvent object when invoking the event, if I change the second parameter in the method subscribing to my event to a MyCustomEvent object I get a compile error. I need to instead explicitly cast the EventArgs object to a MyCustomEvent before I can access any additional fields/methods etc inside the class.

When working with an object that has a lot of different events, each one having a unique related custom EventArgs class, keeping track of what the EventArgs in each method subscribing to each event needs to be casted to can get a bit messy.

It would be a lot easier if I could pass my custom EventArgs class directly into the methods subscribing to my event.

Is this a possibility?

Thanks

  • 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-07T20:41:29+00:00Added an answer on June 7, 2026 at 8:41 pm

    Instead of declaring your event as being of type EventHandler, create a new delegate that specifically uses your custom event args class:

    public delegate void MyEventHandler(object sender, MyEventArgs args);
    public event MyEventHandler MyEvent;
    

    Now you can pass the arguments directly without casting.

    The other option is to use the generic EventHandler<T>:

    public event EventHandler<MyEventArgs> MyEvent;
    

    I prefer the former, since you can then give the delegate type a more descriptive name, though the latter is a bit quicker.

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

Sidebar

Related Questions

Can I register a .Net COM class with the SingleUse -flag? The reason I
Can anyone explain me what's going on? I use this method - (BOOL)shouldAutorotateToInterfaceOrientation:( UIInterfaceOrientation)interfaceOrientation
Can I open my custom developed form [instead of native one] when user opens
can any one tell me how can change this java code into objective c.is
Can you set the internal [[Class]] property of an ECMAScript object?
Can anyone let me know how can we change the value of kendo combobox
Can I order my users in the database, so I don't have to say
Can I change the field public virtual ClassOne ClassOne { get; set; } to
Can I authenticate with just Google account username and password instead of using OAuth?
Can any one tell, how to get the result of LINQ query contains group

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.