Hi, been working on this project and just confusing the heck out of myself with classes and stuff and was hoping maybe someone could lend a hand on what I might be doing wrong?

The general idea of this is to get the arrow (int 231-234) to point in the direct that the use clicked (n/s/w/e) and then when they press go 1 or go 10 it goes the direction selected and go either 1 or 10. But I have to limit it from going over 100 in any direct and show a message that they can't do that. And on load it set to 0,0 and set to north which I believe I have figured already.

General I'm confused on the class\method stuff, which is below, I think I got it right, but for some reason on my form I can't get it to find the getDirection function just errors up on all that.
I'm not sure if I coded something wrong or what, suggested hints or tricks would be helpful.
Thanks in advance.

Main Form:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace simple_robot
{
    public partial class Robot : Form   
    {
        Robot myRobot = new Robot();
        public Robot()
        {
            InitializeComponent();
            pointer.Text = myRobot.getDirection().ToString();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void robotbox_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void label1_Click_1(object sender, EventArgs e)
        {
            
        }

        private void northBtn_Click(object sender, EventArgs e)
        {
            myRobot.setDirection(231);
            pointer.Text = myRobot.getDirection().ToString();
            
        }

        private void westBtn_Click(object sender, EventArgs e)
        {
            myRobot.SetDirection(232);
            pointer.Text = myRobot.getDirection().ToString();
        }

        private void southBtn_Click(object sender, EventArgs e)
        {
            myRobot.SetDirection(233);
            pointer.Text = myRobot.getDirection().ToString();
        }

        private void eastBtn_Click(object sender, EventArgs e)
        {
            myRobot.SetDirection(234);
            pointer.Text = myRobot.getDirection().ToString();
        }

        private void label1_Click_2(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.OnClick = myRobot.getX(myRobot.getX() + 1);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.OnClick = myRobot.getX(myRobot.getX() + 10);
        }
    }
}

robot.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace simple_robot
{
    class robot
    {
        private int direction;
        private int x;
        private int y;

        public void setDirection(int newDirection)
        {
            direction = newDirection;
        }

        public int getDirection(int direction)
        {
            return direction; 
        }

        public int getX()
        {
            return x;
        }

        public int getY()
        {
            return y;
        }

        public void move(int unitToMove)
        {
        }

        public robot()
        {
            x = 0;
            y = 0;
            direction = 231;
        }

    }
}

Whew!

Just going to concentrate on your robot class here.
Find out what automatic properties are.
I rewrote it so:

class robot
    {
        public robot() // Default constructor
        {
            location = new Point(); // Sets location.X=0 and location.Y=0          
            direction = 231;
        }

        public int direction { get; set; } //auto property
        public Point location { get; set; } //auto property

        public void move(int unitToMove)        
        {        
        }              
    }

Use this class like this:

robot Robby = new robot();
            Point P = new Point();
            P = Robby.location; // Get location of Robby

            Point P2 = new Point();
            P2.X = 23; P2.Y = 100;
            Robby.location = P2; // Set location of Robby

Whew!

Just going to concentrate on your robot class here.
Find out what automatic properties are.
I rewrote it so:

class robot
    {
        public robot() // Default constructor
        {
            location = new Point(); // Sets location.X=0 and location.Y=0          
            direction = 231;
        }

        public int direction { get; set; } //auto property
        public Point location { get; set; } //auto property

        public void move(int unitToMove)        
        {        
        }              
    }

Use this class like this:

robot Robby = new robot();
            Point P = new Point();
            P = Robby.location; // Get location of Robby

            Point P2 = new Point();
            P2.X = 23; P2.Y = 100;
            Robby.location = P2; // Set location of Robby

I'll play around with this, why the heck doesn't my book cover this, idk but wish it did because this makes more sense then C# 2.0 way ~_~.
Thanks, although confused on the second part of the code where do you work with that, I'm guessing the form itself?

This is indeed example code in a form.

This is indeed example code in a form.

ok cool, I'm going to keep trying to solve the project with the C# 2.0 way because idk what is required for the assignment, I'm figuring follow the way the book is teaching :S. SO going to have to use the old method of coding C# 2.0 crap. But if I give up on that I'll go with this way which seems a lot simpler.

I'm going to feel slightly bad for wasting a bit of your time, I figured a big issue I had, one of my methods was called setDirection if you look in my form I put it as SetDirection and was causing HUGE issues. I think I'm more on track now with my form. I'll post if I need some help again, but I think I'm figure out the C#2.0 way lol.

This is how I think your complete robot class might look like:

class robot
    {      
        public enum Direction
        {
            North, West, South, East
        }

        public robot() // Default constructor
        {
            location = new Point(); // Sets location.X=0 and location.Y=0          
            direction = Direction.North;
        }

        public Direction direction { get; set; }
        public Point location { get; set; }

        public void Move(int unitToMove)        
        {
            Point P = new Point();

            switch (direction)
            {
                case Direction.North:
                    P.X = location.X;
                    P.Y = location.Y - unitToMove;
                    break;
                case Direction.West:
                    P.X = location.X - unitToMove;
                    P.Y = location.Y;
                    break;
                case Direction.South:
                    P.X = location.X;
                    P.Y = location.Y + unitToMove;
                    break;
                case Direction.East:
                    P.X = location.X + unitToMove;
                    P.Y = location.Y;
                    break;
                default:
                    break;
            }
            location = P;
        }

        public void Draw(Graphics G)
        {
            // Our robot will look like a red ball
            // but you could draw anything here
            SolidBrush redBrush = new SolidBrush(Color.Red);
            Size S = new Size(40,40);
            Rectangle R = new Rectangle(location, S);
            // Make a red ball size 40,40 and put it at location
            G.FillEllipse(redBrush, R);
        }
    }

This is how I think your complete robot class might look like:

class robot
    {      
        public enum Direction
        {
            North, West, South, East
        }

        public robot() // Default constructor
        {
            location = new Point(); // Sets location.X=0 and location.Y=0          
            direction = Direction.North;
        }

        public Direction direction { get; set; }
        public Point location { get; set; }

        public void Move(int unitToMove)        
        {
            Point P = new Point();

            switch (direction)
            {
                case Direction.North:
                    P.X = location.X;
                    P.Y = location.Y - unitToMove;
                    break;
                case Direction.West:
                    P.X = location.X - unitToMove;
                    P.Y = location.Y;
                    break;
                case Direction.South:
                    P.X = location.X;
                    P.Y = location.Y + unitToMove;
                    break;
                case Direction.East:
                    P.X = location.X + unitToMove;
                    P.Y = location.Y;
                    break;
                default:
                    break;
            }
            location = P;
        }

        public void Draw(Graphics G)
        {
            // Our robot will look like a red ball
            // but you could draw anything here
            SolidBrush redBrush = new SolidBrush(Color.Red);
            Size S = new Size(40,40);
            Rectangle R = new Rectangle(location, S);
            // Make a red ball size 40,40 and put it at location
            G.FillEllipse(redBrush, R);
        }
    }

Damn was pretty close to this. the switch direction part is what I was getting caught up on just now. I'm using a label that using Wingdings font with int 231-234 to cause the arrow, that is the only thing I can't figure out. Thank you again.

I should also put a Panel on your Form, and use the Paint event of that Panel, so you can have a Graphics object to draw the robot in.
Seems to me much better than moving around a Label with a strange Font. Albeit, I must admit it was a nice try :)

I should also put a Panel on your Form, and use the Paint event of that Panel, so you can have a Graphics object to draw the robot in.
Seems to me much better than moving around a Label with a strange Font.

I know I'd love that also, but the assignment calls to use this retarded looking wingdingy thing.. I'm sure if I did a wonderful paint and all that it would look cool, but probably get an F for failing directions. I'm just trying to get through this stuff because I have a handle on it, but the assignments are 1 week long and are 50% not covered in the book which is why I come here for help :S.

OK we stick to your assignment but use this modified enumeration:

// fill in your codes here I don't know the exact ones
        // it is better to use Direction.North than 231 or whatever
        public enum Direction
        {
            North = 231,
            West = 232,
            South = 233,
            East = 234
        }

OK we stick to your assignment but use this modified enumeration:

// fill in your codes here I don't know the exact ones
        // it is better to use Direction.North than 231 or whatever
        public enum Direction
        {
            North = 231,
            West = 232,
            South = 233,
            East = 234
        }

Ok I'm following you on this now its making sense. I'll handle the number once I can figure what they show us as. My forms a mess at the moment lol.
I figured to upload the general idea of what I'm to do if that helps any. I'll keep stabbing away at my form and see if I can get any of the buttons to work.

I already seem to have filled in a great deal of your assignment, before I even read it! Now don't start thinking I am some kind of genius guru, I am not ;) Try to make something out of your form right now. It is getting very late over here see ya tomorrow! Succes!

Ok updated my code a bit:

robot.cs

using System.Drawing;
class robot
{
    //set each value to the wingdings int arrow.
    public enum Direction
    {
        North = 233,
        West = 231,
        South = 234,
        East = 232
    }

    public robot() // Default constructor
    {
        location = new Point(); // Sets location.X=0 and location.Y=0          
        direction = Direction.North;
    }

    public Direction direction { get; set; }
    public Point location { get; set; }

    public void Move(int unitToMove)
    {
        Point P = new Point();

        switch (direction)
        {
            case Direction.North:
                P.X = location.X;
                P.Y = location.Y - unitToMove;
                break;
            case Direction.West:
                P.X = location.X - unitToMove;
                P.Y = location.Y;
                break;
            case Direction.South:
                P.X = location.X;
                P.Y = location.Y + unitToMove;
                break;
            case Direction.East:
                P.X = location.X + unitToMove;
                P.Y = location.Y;
                break;
            default:
                break;
        }
        location = P;
    }
}

robot_form:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SimpleRobotForm
{
    public partial class RobotForm : Form
    {
       
        public RobotForm()
        {
            InitializeComponent();
        }
        

        private void RobotForm_Load(object sender, EventArgs e)
        {

        }

        private void northBtn_Click(object sender, EventArgs e)
        {
            
        }

        private void westBtn_Click(object sender, EventArgs e)
        {
          
        }

        private void eastBtn_Click(object sender, EventArgs e)
        {
            
        }

        private void go1Btn_Click(object sender, EventArgs e)
        {
            location temp = arrowLlb.Location;
            temp.X = arrowLlb.Location.X - 1;
            temp.Y = arrowLlb.Location.Y - 1;
            arrowLlb.Location = temp;
        }

        private void go10Btn_Click(object sender, EventArgs e)
        {
            location temp = arrowLlb.Location;
            temp.X = arrowLlb.Location.X - 10;
            temp.Y = arrowLlb.Location.Y - 10;
            arrowLlb.Location = temp;
        }

        private void exitBtn_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void southBtn_Click_1(object sender, EventArgs e)
        {
            
        }

        private void pointer_Click(object sender, EventArgs e)
        {
           
        }
    }
}
private void go1Btn_Click(object sender, EventArgs e)
        {
            location temp = arrowLlb.Location;
            temp.X = arrowLlb.Location.X - 1;
            temp.Y = arrowLlb.Location.Y - 1;
            arrowLlb.Location = temp;
        }

        private void go10Btn_Click(object sender, EventArgs e)
        {
            location temp = arrowLlb.Location;
            temp.X = arrowLlb.Location.X - 10;
            temp.Y = arrowLlb.Location.Y - 10;
            arrowLlb.Location = temp;
        }

I don't know what I'm doing wrong with the location temp = arrowLlb.Location code, it gives me an error about "location" couldn't be found, blah blah. Tired to ask my friend and he wasn't much help signed off on me lol.

The things you do sugest you are doing some effort, that is good!
I give you my start of the solution, hope you learn from it.
It is commented here and there, if you have questions : ask.
The robot class:

namespace simple_robot
{
    // fill in your codes here I don't know the exact ones
    // it is better to use Direction.North than 231 or whatever
    // on my system the code for arrow north is 233 and not 231
    // try to figure out the rest of the codes for yourself
    public enum Direction //can define an enum outside a class, thanks Scott!!!
    {
        North = 231,
        West = 232,
        South = 233,
        East = 234
    }

    class robot
    {      
        // your assignment says this has to be a public field instead of
        // a property, so here it is.
        public Direction direction;

        public robot() // Default constructor
        {
            location = new Point(); // Sets location.X=0 and location.Y=0          
            direction = Direction.North;
        }

        // commented out: direction is now a public field and no property anymore
        //public Direction direction { get; set; } 
        public Point location { get; set; }

        public void Move(int unitToMove)        
        {
            Point P = new Point();

            switch (direction)
            {
                case Direction.North:
                    P.X = location.X;
                    P.Y = location.Y - unitToMove;
                    break;
                case Direction.West:
                    P.X = location.X - unitToMove;
                    P.Y = location.Y;
                    break;
                case Direction.South:
                    P.X = location.X;
                    P.Y = location.Y + unitToMove;
                    break;
                case Direction.East:
                    P.X = location.X + unitToMove;
                    P.Y = location.Y;
                    break;
                default:
                    break;
            }
            location = P;
        }

        // TO DO:
        // to be changed by something like this
        // public void Draw(Panel P)
        public void Draw(Graphics G)
        {
            // Our robot will look like a red ball
            // but you could draw anything here
            SolidBrush redBrush = new SolidBrush(Color.Red);
            Size S = new Size(40,40);
            Rectangle R = new Rectangle(location, S);
            // Make a red ball size 40,40 and put it at location
            G.FillEllipse(redBrush, R);
        }

        // we override the ToString method 
        // and return a text for the label that represents the robot
        // perhaps not the best way to do it, but it works.
        public override string ToString()
        {
            // direction is an enum and used as an ASCII code,
            // we cast it to a char and make it into a string
            return ((char)direction).ToString();
        }
    }
}

The forms class:

using System;
using System.Windows.Forms;

namespace simple_robot
{
    public partial class Form1 : Form
    {
        private robot Robby = new robot();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // will set Robby pointing North
            // because it is initialized as such in the constructor
            robotLbl.Text = Robby.ToString();
            // this is all we need to get {X=0,Y=0} on the screen
            positionLbl.Text = Robby.location.ToString();
        }

        private void northBtn_Click(object sender, EventArgs e)
        {
            // set direction of the robot to north
            Robby.direction = Direction.North;
            // turn Robby in the right direction, so set the label
            robotLbl.Text = Robby.ToString();
        }

        private void go1Btn_Click(object sender, EventArgs e)
        {
            Robby.Move(1);
            // adjust position info
            positionLbl.Text = Robby.location.ToString();
        }  
    }
}

You should fill in the right codes in the enum, add the other direction and go buttons. Should be easy by now;)
Also included a picture of what my form looks like.
As you can see it is not much. I usually work that way: begin small, see if it works and then add more things. Good luck!

Thank you again for the example coding. I looked at it and reread through my book and made a lot more sense. I have managed to get a working robot, and saw where I've been going wrong, did define certain objects in the form, and set certain things to string.
I did borrow the override method, its more functional then having to convert each single string to the WingDings arrow via Uni-code or char value, I can see though where it might cause problems for other project uses.
Being I have to limit the movement to 100< any direction and if it goes 101> or higher show a messagebox, would I use standard validation, that is what I'm guess and playing with.

First let's add a Panel to the Form. I called mine roboworldPnl and gave it a Size of 200,200. Now drop the robotlabel on to it.
The robot no longer lives on the form but on the panel now.
These are the methods I changed:

// TO DO:
        // to be changed by something like this
        public void Draw(Panel P)
        {
            // coordinate translation
            // can you explain why this is needed?
            Point L = new Point(location.X + P.Width / 2, location.Y + P.Height / 2);
            // because we dropped one control on the panel
            // we know that the first control in the Controls collection 
            // of the Panel Controls[0] must be our robot label
            P.Controls[0].Location = L;
        }
private void go1Btn_Click(object sender, EventArgs e)
        {
            Robby.Move(1);
            Robby.Draw(roboworldPnl); //added, change the other go button also
            // adjust position info
            positionLbl.Text = Robby.location.ToString();
        }
private void Form1_Load(object sender, EventArgs e)
        {
            // will set Robby pointing North
            // because it is initialized as such in the constructor
            robotLbl.Text = Robby.ToString();
            // Draw Robby for the first time
            Robby.Draw(roboworldPnl);
            // this is all we need to get {X=0,Y=0} on the screen
            positionLbl.Text = Robby.location.ToString();
        }

You see, I always try to move in small steps and see if they work.
This is far from perfect but we will get there. I know the positionLbl is not working correctly but we'll fix that. I also expect an answer from you to the question I posed in the Draw method.

// can you explain why this is needed?
            Point L = new Point(location.X + P.Width / 2, location.Y + P.Height / 2);

It controls the current location of the Robot, and I believe because we the panel is set to 200,200 the P.width is dived by 2 so technically the Panel is really 100,100?
I see it creating and making the arrow go in the direction N/S/W/E and then when the Go1\G10 is clicked animates the arrow (moves it to) the new postion depending on Go1\Go10.

That how I see it working.

Well, not quite. It transforms the coordinate system of the Panel.
Normally point (0,0) is at the top,left corner of the Panel, just like it is in a Form. By using this transformation you put the point (0,0) or (X,Y) at the center of the Panel, the place where your robot comes alive.
Try to leave P.Width/2 out and see what happens!

Well, not quite. It transforms the coordinate system of the Panel.
Normally point (0,0) is at the top,left corner of the Panel, just like it is in a Form. By using this transformation you put the point (0,0) or (X,Y) at the center of the Panel, the place where your robot comes alive.
Try to leave P.Width/2 out and see what happens!

I understand, I was looking around because the panel says Top Left, so normally if you don't define something it will go with the panel values.
And tested with out the P.Width/2 it shoves it all the way to the left. meaning that the Height and Width control the center point of the arrow.

Does the Panel have any special validation, like a TimeDate Controler has? I'm just playing around with validation to see if I can limit the movement to 100 any direction and get a message to come up.

Just as any control a Panel has a Validating and Validated event, but IMO they are of not much use here. I should check the limits in every go button test if you would pass 100 or -100 in a separate method. This passing would depend on the direction so you could use a switch statement. Coming to think of it, you could probably do this in the Move method of your robot!
The location property of your robot contains the REAL Panel coordinates. When you Draw you shift these by adding half width and height so that 0,0 is in the middle of the Panel.
Now to show these in the position Label you have to substract half width and height. For the Y coordinate you also have to change sign!
The attached doc will make it clearer I hope.
Now for your border testing you could use both, it is up to you which you prefer. Succes.

Just as any control a Panel has a Validating and Validated event, but IMO they are of not much use here. I should check the limits in every go button test if you would pass 100 or -100 in a separate method. This passing would depend on the direction so you could use a switch statement. Coming to think of it, you could probably do this in the Move method of your robot!
The location property of your robot contains the REAL Panel coordinates. When you Draw you shift these by adding half width and height so that 0,0 is in the middle of the Panel.
Now to show these in the position Label you have to substract half width and height. For the Y coordinate you also have to change sign!
The attached doc will make it clearer I hope.
Now for your border testing you could use both, it is up to you which you prefer. Succes.

I understand the real and fiction values of the panel. The problem I'm running into is I don't understand if I should use a:

get
{
if (location.X > 100 && location.Y > -100)
{
throw new ArgumentException("You can't do that!");
}

in the robot.cs or for the go1\10 buttons.

The book shows how to through an argument exception, but not for something like moving an object more for an indexer that validates data and throws an argument.
Or if I just throw an arguementExcept with try if else finally?

Why not make your own robotexception class?

class RobotException : Exception
{
   public string  robotstr;
   public RobotException(string message)
      : base(message)
   {
        robotstr = message;
   }
}

Use it:

try
            {
                //something went wrong so throw exception
                throw new RobotException("no no");
            }
            catch (RobotException ex)
            {
                MessageBox.Show(ex.robotstr);              
            }
commented: tenacious ddanbe! +4

form:

private void go10Btn_Click(object sender, EventArgs e)
        {
            try
            {
                throw new robotexception("You can't go that far; limited to 100units of movement");
            }
            catch (robotexception ex)
                {
                    MessageBox.Show(ex.robotstr);
                }
            //Go 10.
            myRobot.Move(10);
            myRobot.Draw(robotpanel);
            //Update location info.
            robotlocationLlb.Text = myRobot.location.ToString();

            
           
        }

robot.cs:

class robotexception : Exception
        {
            public string robotstring;
            public robotexception(string message)
                : base(message)
            {
                robotstring = message;
            }
        }

Nope my stuff still isn't working, but I'm going to try to just work on getting something to at least validate if it goes over 100 and I'd be happy lol

form:

private void go10Btn_Click(object sender, EventArgs e)
        {
            try
            {
                throw new robotexception("You can't go that far; limited to 100units of movement");
            }
            catch (robotexception ex)
                {
                    MessageBox.Show(ex.robotstr);
                }
            //Go 10.
            myRobot.Move(10);
            myRobot.Draw(robotpanel);
            //Update location info.
            robotlocationLlb.Text = myRobot.location.ToString();

            
           
        }

robot.cs:

class robotexception : Exception
        {
            public string robotstring;
            public robotexception(string message)
                : base(message)
            {
                robotstring = message;
            }
        }

Nope my stuff still isn't working, but I'm going to try to just work on getting something to at least validate if it goes over 100 and I'd be happy lol

Been playing with for a while and just keep getting a namespace error about it. I would normally think the validation is easier, but everything I've tired to do to get this retarded project to show message once X or Y hits 100 or -100 doesn't work. (this proves me I'm not going to be a programmer in life lol).

My friend took one look at my code and said:
"your putting your if \ else in the wrong place"
I put it at the location = P... And freaking work. I drove myself nuts for nothing on the validation :(. Now to relax and try to regrow some of my hair :S.
Thank you so much again for help and direction on this.

You could also use the Abs method to ease your testing of the fictive coordinates. Like in : if (Math.Abs(X)>100)...
Good luck and happy programming!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.