Hi,
I am getting "Delegates must be of the same type." Exception in C# windows application. I am using .net 2.0. I searched in google, but I couldn't get correct solution. please help me..
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
srinathPosted Sep 7, 2009, 3:52 AM
I am trying to do a numberpad which is useful for both calculator and mobile phone using delegates and events. I wrote this code in user control.
public delegate void NumberArgumentHandle(object sender, NumberArgs args);
public delegate void MathOperationHandler(object sender, Math m);
public delegate void AssignmentHandler(object sender,Assign a);
public delegate void CharacterArgumentHandler(object sender, Char cargs);
public delegate void CancelArgumentHandler(object sender, Cancel c);
public object key = new object();
double f;
double y;
string z;
double x = double.MaxValue;
//double o = double.MaxValue;
public event NumberArgumentHandle NumberSelected
{
add
{
Events.AddHandler(key, value);
}
remove
{
Events.RemoveHandler(key, value);
}
}
public event MathOperationHandler Operators
{
add
{
Events.AddHandler(key, value);
}
remove
{
Events.RemoveHandler(key, value);
}
}
public event CharacterArgumentHandler Character
{
add
{
Events.AddHandler(key, value);
}
remove
{
Events.RemoveHandler(key, value);
}
}
public event AssignmentHandler Assignment
{
add
{
Events.AddHandler(key, value);
}
remove
{
Events.RemoveHandler(key, value);
}
}
public event CancelArgumentHandler Cancelop
{
add
{
Events.AddHandler(key, value);
}
remove
{
Events.RemoveHandler(key, value);
}
}
and in Windows Application - form1.cs, I have
void userControl11_NumberSelected(object sender, NumberPad.NumberArgs args)
{
args.Number.ToString();
}
void usercontrol11_Assignment(object sender, NumberPad.Assign a)
{
a.Assign1.ToString();
}
void usercontrol11_Cancelop(object sender, NumberPad.Cancel c)
{
c.Char1.ToString();
}
void usercontrol11_Character(object sender, NumberPad.Char cargs)
{
cargs.Char2.ToString();
}
void usercontrol11_Operators(object sender, NumberPad.Math m)
{
m.Number1.ToString();
}
and in form1.designer.cs, I have
this.userControl11.NumberSelected += new NumberPad.UserControl1.NumberArgumentHandle(this.userControl11_NumberSelected);
this.userControl11.Assignment += new NumberPad.UserControl1.AssignmentHandler(this.usercontrol11_Assignment);
this.userControl11.Cancelop += new NumberPad.UserControl1.CancelArgumentHandler(this.usercontrol11_Cancelop);
this.userControl11.Character += new NumberPad.UserControl1.CharacterArgumentHandler(this.usercontrol11_Character);
this.userControl11.Operators += new NumberPad.UserControl1.MathOperationHandler(this.usercontrol11_Operators);
Actually I am writing a code in usercontrol and calling Usercontrol in form1. At runtime, I am getting a System.Argument exception with message "Delegates must be of the same type." near events.addhandler(key,value) for the event AssignmentHandler.
Please help me..
Roei BarPosted Sep 7, 2009, 3:35 AM