Solved

Class Library Delegate and Event Issues?

Posted on 2012-12-27
6
266 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
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…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

777 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