user2657462
user2657462

Reputation: 131

Calling objects, methods and properties from one class and displaying them with information in another

I have a class called BaseRobot. Containing the following code:

//=== Defines the possible orientation of the robot.
//=== Note the order is important to allow cycling to be performed in a meaningful manner.
public enum Compass
{
    North, East, South, West
};

//=== The basic robot.
public class BaseRobot
{
    //--- The behaviour properties that were identified, together with associated state.
    //--- The robot identification number.
   private int mId;
   public int id
   {
       get { return mId; }
   }

   //--- the direction in which the robot is currently facing.
   private Compass mOrientation;
   public Compass Orientation
   {
       get { return mOrientation; }
       set { mOrientation = value; }
   }

   //--- The robot's current position.
   private Point mPosition;
   public Point Position
   {
       get { return mPosition; }
   }


   //--- the robot's home position, where it was originally created.
   public Point mHome;
   public Point Home
   {
       get { return mHome; }
   }

    //--- Turn the orientation left (anti-clockwise) or right (clockwise).
    //--- Implementation relies on the N, E, S, W ordering of the enumeration values to allow the arithmetic to work.

    public void TurnLeft()
    {
        --mOrientation;
        if (mOrientation < 0) mOrientation = Compass.West;
    } // end turnLeft method.    

    public void TurnRight()
    {
        mOrientation = (Compass)(((int)mOrientation + 1) % 4);
    } // end turnRight method.

    //--- Move one unit forward in the current orientation.
    public void Move()
    {
        switch (mOrientation)
        {
            case Compass.North: mPosition.Y++; break;
            case Compass.East: mPosition.X++; break;
            case Compass.South: mPosition.Y--; break;
            case Compass.West: mPosition.X--; break;
        }
    } // end Move method.

    //--- Constructor methods.
    public BaseRobot(int aId)
    {
        mId = aId;
        mHome.X = 0;
        mHome = new Point(0, 0);
        mPosition = mHome;
    }
    public BaseRobot(int aId, int aX, int aY)
    {
        mId = aId;
        mHome = new Point(aX, aY);
        mPosition = mHome;
    } // end BaseRobot constructor methods.
} 

In the program class, i am looking to call the objects, methods and properties from this code to display information regrading the robot's status, such as robot home, orientation and position. I am looking for the console to display something like this:

Robot has home at <0,0> It is facing North and is currently at <0,0>

Any ideas of how i can achieve this?

Upvotes: 1

Views: 152

Answers (3)

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236248

First, I suggest you to read about Auto-Implemented Properties. That will make your code look like:

public Point Home { get; private set; }
public Point Position { get; private set; }
public Compass Orientation { get; private set; }

Now back to formatting your robot to string. You can override ToString() method of robot class:

public override string ToString()
{
   return String.Format("Robot has home at {0} It is facing {1} and is currently at {2}",
                        mHome, mOrientation, mPosition);
}

Or simply pass robot instance to following method:

public void WriteToConsole(BaseRobot robot)
{
   Console.WriteLine("Robot has home at {0} It is facing {1} and is currently at {2}",
                     robot.Home, robot.Orientation, robot.Position);
}

NOTE: By default System.Drawing.Point class will be converted to string {X=42,Y=8}. If you need it formatted as <42,8> then you should manually provide values for X and Y like this:

String.Format("Robot has home at <{0},{1}> It is facing {2} and is currently at <{3},{4}>", 
              Home.X, Home.Y, Orientation, Position.X, Position.Y);

Or if Point is your own class, then simply override it's ToString() method:

return String.Format("<{0},{1}>", X, Y);

Upvotes: 1

DavidN
DavidN

Reputation: 5197

Format strings can be used to achieve this easily.

You can use Console.Writeline, like so:

// Assumes a Robot r exists already
Console.Writeline("Robot has home at <{0},{1}> and is facing {2} and is currently at <{3},{4}>", r.Home.X, r.Home.Y, r.Orientation, r.Position.X, r.Position.Y);

Overriding ToString() is another way of doing this:

public override string ToString(){
    return string.Format("Robot has home at <{0},{1}> and is facing {2} and is currently at <{3},{4}>", Home.X, Home.Y, Orientation, Position.X, Position.Y);
}

Overriding ToString() doesn't seem correct here, however, because you're displaying status information about a Robot that isn't really the "string representation of a robot", which is what ToString() should be supplying. I'd either use the Console.Writeline method above, or have a string Status property on Robot to get this type of information.

Upvotes: 0

Justin Harvey
Justin Harvey

Reputation: 14672

Try Console.WriteLine, calling your class methods to get the argument values, see here for more

http://msdn.microsoft.com/en-us/library/aakt1eab.aspx

Upvotes: 0

Related Questions