Solved

Class Library Delegate and Event Issues?

Posted on 2012-12-27
6
263 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
Comment Utility
then, you must initialice the object in your dll
0
 
LVL 74

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 500 total points
Comment Utility
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
Comment Utility
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:pointeman
Comment Utility
Q, Anyway to use a cool ?? operator instead?

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

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
Comment Utility
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
Comment Utility
Cool, thanks again
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Mutually exclusive checkbox in a gridview 18 40
Build JSON from table records 17 38
LINQ question 7 20
Get String split 5 31
Article by: Ivo
Anonymous Types in C# by Ivo Stoykov Anonymous Types are useful when  we do not need to follow usual work-flow -- creating object of some type, assign some read-only values and then doing something with them. Instead we can encapsulate this read…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

744 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now