Solved

Class Library Delegate and Event Issues?

Posted on 2012-12-27
6
268 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C# Gridview 1 64
Changes to XXX.exe.config file are not retrieved on restart. 3 49
"The resource cannot be found" 1 22
Stop Git from being my repository 1 18
Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

740 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