Solved

Class Library Delegate and Event Issues?

Posted on 2012-12-27
6
267 Views
Last Modified: 2012-12-27
I usually built a Class Libary within a Windows Forms project. I can test the class by using buttons and textboxes. I send the 'Status' messages to Form1 using a Delegate and Event as illustrated in the code example below. The problem arises when deploying the Class Libarary to be used with another executable. The unused Delegate causes an 'Object Reference' error if it's not instanctiated in the new executable. I want the option for class library status message returns if needed, but don't want to demand the delegate instanciation.

   [class library (.dll)]
   public delegate void JobManagerStatusEventHandler(string statusMessage);

    public static class JobSetup
    {
        public static event JobManagerStatusEventHandler StatusMessage;
    }
0
Comment
Question by:pointeman
  • 2
  • 2
  • 2
6 Comments
 
LVL 8

Expert Comment

by:Crashman
ID: 38723887
then, you must initialice the object in your dll
0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 500 total points
ID: 38723952
I'm not sure I fully understand your question, but in C# you should be checking to see if any event handlers have been attached to your event by comparing the event to null prior to invoking it.

e.g.

public static void SomeMethod()
{
    if (StatusMessage != null)
    {
        StatusMessage("Hello World!");
    }
}

Open in new window

0
 
LVL 8

Expert Comment

by:Crashman
ID: 38723986
you must create the object, because the class is static is created in memory and you must create one instance for related objects

http://msdn.microsoft.com/en-us/library/79b3xss3%28v=vs.80%29.aspx
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

Author Comment

by:pointeman
ID: 38724060
Q, Anyway to use a cool ?? operator instead?

        StatusMessage(??  : "Hello World!");
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 38724161
What would you use it for? The coalesce operator ( ?? ) returns the first non-null argument in its parameter list. You haven't really shown something that can be null (outside of the event). If you had something like:

public delegate void JobManagerStatusEventHandler(string statusMessage);
private static string message = null;

public static class JobSetup
{
    public static event JobManagerStatusEventHandler StatusMessage;
}

public static void SomeMethod()
{
    if (StatusMessage != null)
    {
        StatusMessage(message ?? "Hello World!");
    }
}

Open in new window


...then sure.
0
 

Author Closing Comment

by:pointeman
ID: 38724219
Cool, thanks again
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question