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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:56:27+00:00 2026-06-16T05:56:27+00:00

I have two classes, and i have to make an event to communicate between

  • 0

I have two classes, and i have to make an event to communicate between these classes.

Class a
{
    public delegate void delegat(int a);
    public event delegat exit;
    ...
    private void a_FormClosed(object sender, FormClosedEventArgs e)
    {
         // My event named exit should run here, but I get exception!
        exit(100);
    }
}

Class b
{
    a instance=new a();
    a.exit+=new a.delegat(my_fun);
    ...
    private void my_fun(int x)
    {
        if(x==100)
        do_smth;
        ...
    }
}

But the thing is that i get exception: “object reference not set to an instance of an object”.
I can’t understand what Am I doing wrong? Where should I make a new instance of this?
Thanks for help!

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

    You are trying to assign the exit event on the class itself and not the instance e.g.

    a.exit += ...
    

    Should be:

    instance.exit += ...
    

    You also aren’t checking whether your exit event has been assigned before attempting to fire it. There are other issues which you haven’t taken into consideration like race conditions.

    Here is a general example of a relatively safe way of handling events e.g.

    public class A
    {
        public delegate void ExitHandler(object sender, int a);
        public event ExitHandler Exit;
        ...
        private void a_FormClosed(object sender, FormClosedEventArgs e)
        {
            OnExit(100);
        }
    
        protected virtual void OnExit(int a)
        {
            // take a reference to the event (incase it changes)
            var handler = Exit;
            if (handler != null)
            {
                handler(this, a);
            }
        }
    
    }
    
    public class B
    {
        private A _a;
    
        public B()
        {
            _a = new A();
            _a.Exit += (sender, value) => my_fun(value);
        }
    
        private void my_fun(int x)
        {
            if(x==100)
                do_smth;
            ...
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say i have the following two classes: public class User { public int ID
Say, we have two classes: public class A { protected static readonly int DefaultValue
I have defined two classes below: public class junk { private BigInteger a =
I have two classes (MVC view model) which inherits from one abstract base class.
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 set up as follows: class Point { protected: double coords[3];
I have two classes, class A and class B. Class A has a mission
Suppose I have a definition for a door: class Door { public void Lock()
I have these two classes set up and I want Entity Framework to automatically

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.