c# - Calling objects, methods and properties from one class and displaying them with information in another -
i have class called baserobot. containing following code:
//=== defines possible orientation of robot. //=== note order important allow cycling performed in meaningful manner. public enum compass { north, east, south, west }; //=== basic robot. public class baserobot { //--- behaviour properties identified, associated state. //--- robot identification number. private int mid; public int id { { return mid; } } //--- direction in robot facing. private compass morientation; public compass orientation { { return morientation; } set { morientation = value; } } //--- robot's current position. private point mposition; public point position { { return mposition; } } //--- robot's home position, created. public point mhome; public point home { { return mhome; } } //--- turn orientation left (anti-clockwise) or right (clockwise). //--- implementation relies on n, e, s, w ordering of enumeration values allow arithmetic 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 1 unit forward in 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 program class, looking call objects, methods , properties code display information regrading robot's status, such robot home, orientation , position. looking console display this:
robot has home @ <0,0> facing north , @ <0,0>
any ideas of how can achieve this?
first, suggest read auto-implemented properties. make code like:
public point home { get; private set; } public point position { get; private set; } public compass orientation { get; private set; } now formatting robot string. can override tostring() method of robot class:
public override string tostring() { return string.format("robot has home @ {0} facing {1} , @ {2}", mhome, morientation, mposition); } or pass robot instance following method:
public void writetoconsole(baserobot robot) { console.writeline("robot has home @ {0} facing {1} , @ {2}", robot.home, robot.orientation, robot.position); } note: default system.drawing.point class converted string {x=42,y=8}. if need formatted <42,8> should manually provide values x , y this:
string.format("robot has home @ <{0},{1}> facing {2} , @ <{3},{4}>", home.x, home.y, orientation, position.x, position.y); or if point own class, override it's tostring() method:
return string.format("<{0},{1}>", x, y);
Comments
Post a Comment