1

I have a label in Form1 I'm trying to modify. Here's my code:

namespace asst5
{
    public partial class Form1 : Form
    {
        Robot robot1 = new Robot();
        public Form1()
        {
            InitializeComponent();
            label2.Location = new Point(100,100);
            label1.Text = label2.Location.ToString();
        }

        private void button7_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            label2.Text = "↑";
            robot1.direction = 1;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            label2.Text = "↓";
            robot1.direction = 2;
        }

        private void east_Click(object sender, EventArgs e)
        {
            label2.Text = "→";
            robot1.direction = 4;
        }

        private void west_Click(object sender, EventArgs e)
        {
            label2.Text = "←";
            robot1.direction = 3;
        }

        private void button6_Click(object sender, EventArgs e)
        {
            robot1.speed = 1;
            robot1.move();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            robot1.speed = 10;
            robot1.move();
        }
    }

    public class Robot
    {
        Form1 frm1 = new Form1();
        public int direction = 1; //1 = north, 2 = south, 3 = west, 4 = east
        public int speed = 1;

        public void move()
        {
            if (direction == 1)
            {
                frm1.label2.Location = new Point(frm1.label2.Location.Y - speed);
            }

            if (direction == 2)
            {
                frm1.label2.Location = new Point(frm1.label2.Location.Y + speed);
            }

            if (direction == 3)
            {
                frm1.label2.Location = new Point(frm1.label2.Location.X - speed);
            }

            if (direction == 4)
            {
                frm1.label2.Location = new Point(frm1.label2.Location.X + speed);
            }
        }
    }
}

Form1 frm1 = new Form1(); is where the stack overflow occurs. I'm sure this isn't a proper way of doing it but when I try to otherwise it tells me 'An object reference is required for the non-static field.'

2
  • random code review comment: instead of instantiating a new point to move the labels, consider just modifying the axis of the existing point, something like: frm1.label2.Location.Y -= speed; Commented Oct 16, 2014 at 4:15
  • Tried that, it claims it's not a variable. "Cannot modify the return value of 'System.Windows.Forms.Control.Location' because it is not a variable." Commented Oct 16, 2014 at 4:29

1 Answer 1

2

You have a recursive declaration.

Form1 instantiates a Robot ... which instantiates a Form1 ... it goes around in circles until you finally blow the stack.

What you want to do, is pass an instance of Form1 to your Robot. Firstly, remove the Form1 frm1 = new Form1(); line. Then, introduce this field:

Form1 frm1;

Then, create a constructor in your Robot:

public Robot(Form1 frm) {
    frm1 = frm; // pass the form in
}

Then, in your Form1 class, instantiate your robot in the constructor by passing in the instance of the form:

Robot robot1;

public Form1()
{
    InitializeComponent();
    label2.Location = new Point(100,100);
    label1.Text = label2.Location.ToString();
    robot1 = new Robot(this); // "this" is the Form1 instance        
}
Sign up to request clarification or add additional context in comments.

4 Comments

I replaced Form1 frm1 = new FOrm1(); with Form1 frm1; and created the constructor in Robot. When I put Robot robot = new Robot(this;) in Form1 I get an error "'this' is not available in the current context."
@sirhotalot You need to instantiate the robot INSIDE the Form() constructor. You can leave it declared where it is since it needs global scope, but you can't instantiate it until you have an instance to pass in, which is in the constructor (or later).
Tried that, I get "The name 'robot' does not exist in the current context" when I try to access it.
Apologies for leaving this answer half-baked. Thanks @RufusL for the fixup.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.