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");
}
}
Typically, to raise an event, you add a method that is marked as protected and virtual (in C#) or Protected and Overridable (in Visual Basic). Name this method On EventName; for example, OnDataReceived .
EventArgs e is a parameter called e that contains the event data, see the EventArgs MSDN page for more information. Object Sender is a parameter called Sender that contains a reference to the control/object that raised the event.
In a previous article we examined the use of events in C#. These provide a manner for objects to signal actions such as state changes or indications that an activity is about to be performed. The previous article explained how to create custom events and link them to one or more subscribers.
C event handlers allow you to interface more easily to external systems, but allowing you to provide the glue logic. The POS includes a small open source C compiler (TCC) which will dynamically compile and link your C code at runtime. Alternatively, you can precompile and ship a DLL/EXE with your functions.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With