Reputation: 69
I have been studying MANY other answers and examples about this and I am just getting more and more confused on how to set this up. I need to raise an event in the Robot class based on the result of the performMove method in the form class. I know I can't raise the event from another class, so what I have obviously doesn't work. But I'm really not grasping how to set this up properly. I have read the delegates and events articles on codeProject, dreamInCode, and in this site, amongst many others. This is for a beginner c# class, and I'm pretty new to this, as I'm sure everyone can tell :)
namespace Assignment12
{
public delegate void ErrorHandler();
public partial class frmRobot : Form
{
Robot moveRobot = new Robot();
public frmRobot()
{
InitializeComponent();
reset_Position();
current_Position_Display();
moveRobot.outOfRange += new ErrorHandler(moveRobot.coor_Within_Range);
}
...
private void performMove()
{
Point loc = lblArrow.Location;
int x = moveRobot.Move_Robot_XAxis(loc.X);
int y = moveRobot.Move_Robot_YAxis(loc.Y);
if (x < -100 && x > 100)
{
moveRobot.outOfRange();
x = loc.X;
}
if (y < -100 && y > 100)
{
moveRobot.outOfRange();
y = loc.Y;
}
this.lblArrow.Location = new Point(x, y);
current_Position_Display();
}
class Robot
{
public event ErrorHandler outOfRange;
...
public void coor_Within_Range()
{
System.Console.WriteLine("TestOK");
}
}
Upvotes: 5
Views: 553
Reputation: 659994
This question is quite confusing.
The question you should be posing to yourself is: who is in charge of declaring and enforcing policy? You have two entities: "form" and "robot". You have some policy about what a legal position is for the robot. What class is responsible for coming up with that policy? Does the robot know when it is out of range, and it informs the form of that fact? Or does the form know when the robot is out of range, and it informs the robot of that fact?
The thing that wishes to be informed is the event listener. The thing that wishes to inform others of a policy violation is the event source. It is totally unclear which one of these things you want to be the listener and which one you want to be the source. But the rule you are violating is clear: the event listener is not the thing that is allowed to say when the event happens. The person listening to the concert does not get to stand up and yell instructions to the pianist about what keys to press! That's the pianist's decision, and the listener merely gets to decide whether to listen or not, and how to react.
If the form gets to decide when the robot is out of range then the robot needs to be the listener. If the robot gets to decide when the form is out of range then the form needs to be the listener. Right now you've got the form being the listener, and yet it is trying to tell the robot when it is out of range.
Upvotes: 13
Reputation: 273179
You don't seem to need an event. Right now it's just a complicated way to call moveRobot.coor_Within_Range()
. Cut out the middleman:
if (x < -100 && x > 100)
{
moveRobot.coor_Within_Range();
x = loc.X;
}
Although Within_Range and outOfRange are curiously opposite names.
You would need an event to inform the Form about something happening in the Robot. I posted an answer here about how to do that.
Upvotes: 2
Reputation: 15024
Your coor_Within_Range
needs to raise the event:
public void coor_Within_Range()
{
System.Console.WriteLine("TestOK");
if (this.outOfRange != null) {
this.outOfRange();
}
}
Then in your Form
class you need to handle the event:
public frmRobot()
{
// snipped
moveRobot.outOfRange += new ErrorHandler(this.oncoor_Within_Range);
}
public void oncoor_Within_Range() {
Console.WriteLine("robot within range");
}
Upvotes: 0