Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

C Sharp Delegates help


  • Please log in to reply
2 replies to this topic

#1 careless25

careless25

  • Members
  • 6 posts
  • OFFLINE
  •  
  • Local time:04:30 AM

Posted 08 June 2011 - 10:41 PM

Hi,

I am learning C# and I am having trouble with one of the assignments. I would appreciate it if someone could give me some feedback and a tutorial if possible. I understand the concept of delegates...i think, but I am not sure what my assignment is asking for.

Here is the sample code we are given:

public interface Robot

{

public void moveForward();

public void turn(float degrees);

}

public
class RobotImpl : Robot {

float xpos, ypos;

float xvel, yvel;

public void moveForward() {

xpos += xvel; ypos += yvel;

}

public void turn(float degrees) {

float speed = 25, heading = 10;

heading += degrees;

xvel = 15; yvel = 15;

}

}

//hard coded option:
Robot r = new RobotImpl();

//Using a factory Method:
Robot r = Factory.newRobot();

Our task is to:
Write the delegate type for the newRobot() method. Create the Factory class and
add a readonly field of that delegate type. Create a SimulationDriver class with a Main
method that calls this factory method and stores the resulting Robot object.

So to create the delegate type it has to be the same return type as the newRobot method, but i dont see any return type for the Robot object. But i was thinking it would be something like:

public delegate void newRobot();?

I am completely lost about what to do about the Factory class...

but I know it has to contain some code similar to:

public class Factory{

private readonly delegate void newRobot(); (not sure about this)

}

I need any help possible, I have midterms in a week and I want to be able to code this!

Thanks :)

C25

BC AdBot (Login to Remove)

 


#2 jeffman1

jeffman1

  • Members
  • 7 posts
  • OFFLINE
  •  
  • Local time:03:30 AM

Posted 10 June 2011 - 12:37 PM

not for sure if read only is right but rest looks right i have taken c++ and java. I havent covered much in delegates though but i will try to help.

#3 careless25

careless25
  • Topic Starter

  • Members
  • 6 posts
  • OFFLINE
  •  
  • Local time:04:30 AM

Posted 10 June 2011 - 02:09 PM

Hmm well I need help on the readonly part as well as the Factory class, I dont know what else to include in that class.

Thanks!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users