Wednesday, 15 June 2011

direct access to base class variables / objects c# -



direct access to base class variables / objects c# -

public class myworld { public int data; public void changedata() { info = 10; } } public class myrobot : myworld { public void robotchangesdata() { //how can create robot alter info in world? } }

i understand (more or less) should not done way, , has been asked one thousand times, every alter should through methods - but:

if remain world , robot example, later on want have method robot like: robot.movebox(25) robot has have access world, object box, , create updates drawing objects (meshes, shapes etc.) thing can come now, pass every method of robot (like movebox, or robotchangesdata) whole world + box + drawing stuff 'ref' , can alter then.. every method robot.movebox(25, ref myworldobject, ref myworldboxes,ref etc etc)

is right way go? or did miss important?

maybe illustration helps:

your robot base of operations class

public class robotbase { protected int data; // reference world protected world _world; public robotbase(world world) { _world = world; } public void changedata() { info = 10; } }

your robot class:

public class robot : robotbase { public robot(world world) : base(world) {} public void robotchangesdata() { //change info in base of operations info = 20; // alter info in world, object passed reference, no need farther "ref" declarations _world.terminate(); } }

your world class:

public class world { public void terminate() { // terminate world! noooess! } }

c#

No comments:

Post a Comment