How do declare FireEvent(...) so I can use it in foo()
With previous beta versions, I can just do this FireEvent(EventHandler^ e), but with beta2 compiler, I get C3918.
Thanks,
hdp.
// class that defines events
public:
event EventHandler^ OnEvent1;
event EventHandler^ OnEvent2;
void FireEvent(...)
{
....
}
void foo()
{
FireEvent(OnEvent1);
FireEvent(OnEvent2);
}
};

What is the syntax for declaring an event as an argument of a function?
Spenceee
http://lab.msdn.microsoft.com/productfeedback/
t.g.
BabuReddyh
C#:
event EventHandler a;
event EventHandler b;
void Fire( EventHandler e)
{
if (e != null)
e(this, new System.EventArgs());
}
void Bar()
{
Fire(a);
Fire(b);
}
Works also in VS2002/2003
Greetings
Jochen
Derek Diamond
For now, I replaced the function with a macro and it seems to works.
ch
I recall that bug. This is by design in the new syntax - Managed Extensions should still work correctly.
In your scenario, it might work better to use delegates as data member in the class, instead of full events, like so:
delegate void EventHandler(int);
ref class EventSource {
public:
EventHandler^ OnEvent1;
EventHandler^ OnEvent2;
void FireEvent(EventHandler^ e)
{
e(10); //fire the event
}
void foo()
{
FireEvent(OnEvent1);
FireEvent(OnEvent2);
}
};
A trivial event (like event EventHandler^ OnEvent1;) is not a datamember, it's an abstraction of a delegate behind the event. Managed Extensions didn't make that distinction, but I think it's an important one.
mrboldt
thanks, hdp.
Tarun_Patel
FabioPoroli
First of all: The example works also in VC2002/2003:
ldsyntax.
__event EventHandler *a;
__event EventHandler *b;
void Fire(EventHandler *e)
{
if (e != NULL)
e(this, __gc new System::EventArgs());
}
void Bar()
{
Fire(a);
Fire(b);
}
And it does not compile (and therefor also not work) in VC2005B2 when compiled with /clr
The second is: In C++/CLI you do not need to check if the event is "null"! You can just call it:
a(this, gcnew System::EventArgs());
But you are right: The following does not work:
event EventHandler ^a;
event EventHandler ^b;
void Fire(EventHandler ^e)
{
if (e != nullptr)
e(this, gcnew System::EventArgs());
}
void Bar()
{
//Fire(a); // generates: "usage requires 'A::Class1::a' to be a data member"
//Fire(b);
a(this, gcnew System::EventArgs());
b(this, gcnew System::EventArgs());
}
So this might be a bug... but I did not read the C++/CLI spec...
---
Greetings
Jochen
Archonn
http://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx feedbackid=ac4f8e6b-c5eb-4ad0-9585-2fd0542a06aa
WastingBody
Care to open a new feedback as a suggestion that this wrong descision be re-visited
AnnyJacky
Can't pass the event EventHandler^ to a parameter coz its declared as an event but not a "Data Member delegate"... (I assume that EventHandler^ is a .net Delegate since you need a delegate when you create an event)
eg.
delegate void Del();
Del^ myEvent1;
event Del^ myEvent2;
void Fire(Del^){};
//Can do this
Fire(myEvent1);
//Causes error C3918
Fire(myEvent2);
What are you trying to achieve anyway
cheers,
Paul June A. Domag
Angusl
hdp.
texlnghorn
Thanks.
double eagle
Under the covers, an event IsA delegate, so I would expect (and want) it to be possible to treat the event as a delegate. Passing the event to a central 'event raising' function is not an unreasonable thing to want to do, IMO.
Does C# allow passing an event to a function that takes the corresponding delegate type