Click here to Skip to main content
15,896,497 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
Hello.

I've this code in my .NET project:
C#
public delegate void CtrlVisible(Boolean v);

inside of a class which doesn't implements EventArgs.
I use it in this way:

rs.ctrlVisible += new clsReportStatus.CtrlVisible(rs_ctrlVisible);


where rs_ctrlVisible is:
C#
public event CtrlVisible ctrlVisible;


How should I fix that problem?

Thanks.

What I have tried:

Tried reading MSFT documentation about event driven development.
Casting instances to object and then converting them to Boolean again.
Posted
Updated 10-Apr-17 5:37am
Comments
[no name] 10-Apr-17 11:18am    
https://msdn.microsoft.com/en-us/library/ms182133.aspx
Member 13118954 10-Apr-17 11:30am    
the first thing I did was RTFM, if I asked here it was because I couldn't solve it by reading the MSFTs documentation

1 solution

"Events are a special kind of multicast delegate that can only be invoked from within the class or struct where they are declared." - MSDN.

What that means is that the signature of a delegate that can be used as an event must match that of all events: an object sender, and a EventArgs derived class instance.
A bool is not suitable: it doesn't match the event signature.

Change your delegate as the error message suggested.
 
Share this answer
 
Comments
Member 13118954 10-Apr-17 11:57am    
Hey @OriginalGriff

But I need to check the value of my boolean variable in order to make a progress bar visible or not.

private void rs_ctrlVisible(bool v)
{
	try
	{
		if (this.InvokeRequired)
		{
			MethodInvoker del = delegate { rs_ctrlVisible(v); };
			this.Invoke(del);
		}
		else
		{
			progressBar1.Visible = v;
		}
	}
	catch
	{

	}
}


How could I achieve that functionality using object and EventArgs arguments?

Regards.

Regards.
OriginalGriff 10-Apr-17 12:11pm    
Declare a new EventArgs class that derives from EventArgs, and put your boolean value inside that.
Member 13118954 10-Apr-17 12:34pm    
Seems to be the best option. I Will investigate more about that.

thank you
OriginalGriff 10-Apr-17 13:57pm    
You're welcome!
Richard Deeming 11-Apr-17 14:01pm    
"the signature of a delegate that can be used as an event must match that of all events"

Technically, that "must" should be a "should". It's perfectly possible - but definitely not recommended - to create events using delegates that don't follow the standard .NET signature - for example, lots of COM libraries do just that.

CA1009 is an FxCop warning, not a compiler error. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900