5

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");

    }
}
Nitesh
  • 2,286
  • 2
  • 43
  • 65
Tammy Hartin
  • 69
  • 1
  • 5
  • Sorry, yes. In performMove, the line moveRobot.outOfRange() raises "Error 2 The event 'Assignment12.Robot.outOfRange' can only appear on the left hand side of += or -= " – Tammy Hartin Apr 18 '12 at 16:59
  • 4
    What's the question? There doesn't appear to be a question in this question, rather just the statement that you don't know how to do what you want to do. Can you describe more clearly what you want to do? For example, "I want to make a method on one class which, when called by another class, causes the first class to fire an event". – Eric Lippert Apr 18 '12 at 16:59
  • I believe the 2nd sentence (I need to raise an event in the Robot class based on the result of the performMove method in the form class.) asks the question. Thanks! – Tammy Hartin Apr 18 '12 at 17:01
  • first error, you can't susbcribe to an event that the same class is raising, there is no point in that, just call the method ;). Another problem, who should raise the event, and who should receive it? – gbianchi Apr 18 '12 at 17:01

3 Answers3

13

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.

Eric Lippert
  • 647,829
  • 179
  • 1,238
  • 2,067
2

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.

Community
  • 1
  • 1
H H
  • 263,252
  • 30
  • 330
  • 514
0

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");
}
smoak
  • 14,554
  • 6
  • 33
  • 33