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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:15:07+00:00 2026-05-27T08:15:07+00:00

I’m making a Class Library and I’m using it inside a Windows Form Program.

  • 0

I’m making a Class Library and I’m using it inside a Windows Form Program.
I would like to handle an event of this library inside the program.
I’m using this code :

Class inside the library :

public class KSEDataServIO
{
    public delegate void IsReadyForUseEventHandler(object sender, IsReadyForUseEventArgs e);
    public event IsReadyForUseEventHandler IsReadyForUse;
    public KSEDataServIO(){
       EvArg = new IsReadyForUseEventArgs("AuthOkay");
       IsReadyForUse(this, EvArg); //This is where i get the issue.
    }
}

And, In the Window Form I’m doing this :

private void button1_Click(object sender, EventArgs e) {
            KSEDataServIO con = new KSEDataServIO();
            con.IsReadyForUse += new KSEDataServIO.IsReadyForUseEventHandler(con_IsReadyForUse);
}

void con_IsReadyForUse(object sender, IsReadyForUseEventArgs e)
{
     MessageBox.Show(e.Etat);
}

I got a NullReferenceException to the line ‘IsReadyForUse(this, EvArg);’ inside the class library.
Any idea ?

  • 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-27T08:15:07+00:00Added an answer on May 27, 2026 at 8:15 am

    Your problem is that you raise the event inside the constructor of KSEDataServIO. At that point nothing has subscribed to that event handler and therefore it raises a null reference exception.

    So one thing is to properly raise the event handler for which this pattern is commonly used:

    public delegate void IsReadyForUseEventHandler(object sender, IsReadyForUseEventArgs e);
    public event IsReadyForUseEventHandler IsReadyForUse;
    
    void OnIsReadyForUse(IsReadyForUseEventArgs e)
    {
        var handler = IsReadyForUse;
        if (handler != null)
        {
            handler(this, e);
        }
    }
    

    Then use it like this to raise the event:

    OnIsReadyForUse(new IsReadyForUseEventArgs("AuthOkay"))
    

    Secondly raising the event inside your constructor doesn’t make much sense as nothing could possibly have yet subscribed to the handler (unless you pass a handler as constructor parameter). You will need to find another trigger where you can raise the event later on.

    Also you should expose a IsReady property in your class. So if a user comes along later it can query the object if it is already ready. If the IsReady event was already raised when you start using the object somewhere then you might miss the event otherwise.

    Edit: You could pass a handler to the constructor if you really wanted to do this:

    public KSEDataServIO(IsReadyForUseEventHandler handler)
    {
       IsReadyForUse += handler;
       OnIsReadyForUse(new IsReadyForUseEventArgs("AuthOkay")); // see pattern above
    }
    

    However as your event provides this as the sender you pass a reference to an object before it has finished executing the whole constructor potentially leading to problems which are hard to track down. If the only place where you are ever going to raise the event is the end of the constructor then you don’t really need it.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I am using Paperclip to handle profile photo uploads in my app. They upload
I'm making a simple page using Google Maps API 3. My first. One marker
I have this code to decode numeric html entities to the UTF8 equivalent character.
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.