-2

I am/was trying to create an event that my main form can subscribe to. This class will eventually be larger, it is for doing info/warning/error info to Windows event viewer (have not done that part yet) but it also sends to the form information. I have used events in many places, but this is the first time I am trying to make one in a static class. I didn't think there would be any issues, but I get this error "raiseEventtoForm: cannot declare instance members inside a static class."

All of my members are static- I am not sure why this is happening.

Am I not able to produce events in a static class? I can't find anything indicating I wouldn't be allowed to.

Edit: Updated delegate to not have static keyword.

public static class HABLAEvents
{
    public delegate void RaiseMessageEvent(string message, Color foreColor, Color backColor);
    public static event RaiseMessageEvent trigger = delegate { };
    public static void MessageEvent(string message, Color foreColor, Color backColor) { trigger(message, foreColor, backColor); }

    public static void raiseEventtoForm(string message, Color fc, Color bc)
    {
        MessageEvent(message, fc, bc);
    }
}

Edit: That same error "cannot declare instance members inside a static class" is also present for trigger and MessageEvent

Nyra
  • 859
  • 1
  • 7
  • 27
  • 1
    I think this line is incorrect: public static delegate void RaiseMessageEvent(string message, Color foreColor, Color backColor); a delegate is actually a type it cant be static... – AK_ Jun 18 '14 at 18:27
  • also the second line should be public static event RaiseMessageEvent trigger ; – AK_ Jun 18 '14 at 18:30
  • Yeah- When I did a clean/build those inline errors went away and it popped up saying that my delegate couldn't have static. I removed that and it built ok. I am testing it to make sure it works ATM. Making edits to post now. – Nyra Jun 18 '14 at 18:32
  • 1
    Just as a tip, always try to take care of the first error the compiler encountered, because after an error sometimes the compiler doesnt understand (fails to parse) the rest of the program. this is especially true for C++ which is by itself not parseable – AK_ Jun 18 '14 at 18:44
  • After making the edits (ie removing that extra static) it works like a charm. Thanks for pointing that out to me :) – Nyra Jun 18 '14 at 18:53
  • wth? Why did this get down voted? I think people down-vote for fun. AK_ and I found and fixed the problem fairly fast, and it was clearly explained. And what I was doing wrong was clearly explained. And now it's corrected... Again... wth? – Nyra Jun 18 '14 at 19:26

1 Answers1

1

here, take an example:

using System;

public static class Test
{
    public static void Main()
    {
        EventsClass.someDelegateEvent += func;
        EventsClass.Raise();

    }


    public static void func(int number){
        Console.WriteLine(number);
    }

}

public static class EventsClass
{
    public delegate void someDelegate(int num);

    public static event someDelegate someDelegateEvent;

    public static void Raise()
    {
        if (someDelegateEvent != null)
        someDelegateEvent(6);
    }
}
AK_
  • 7,981
  • 7
  • 46
  • 78
  • Thanks for your help! I have it working. I edited my post to show it working correctly now. I can't answer my own for 2 days. – Nyra Jun 18 '14 at 19:27