You haven't created an instance of the Random class within your Dog class:
public int RaceLenght =20;
public int StartPosition = 0;
public PictureBox IMG;
public int Position;
public Random Rnd;
You could add the
public Random Rnd = new Random();
But that is a bit of a nuicence, since there is a very good chance that all Dogs will end up with the same sequence of random numbers. Instead, I would create a
random class and use that instead for all instances:
public static Random Rnd = new Random();
int i = Dog.Rnd.Next(1, 4);
BTW: I would strongly suggest that your fields should be properties - it is considered bad practice to expose fields directly, as it means you can't easily change the internals of your class without considering the effects on the outside world.
"ok im sorry wasting your time, i will now explaining it. its simple, i just want a programm that tells what dog was first on finish line. Every dog has another speed. I want to write it with classes. But now its the problem that all doggs are running, but not at same time.
Here is the method:
public Point p = new Point();
public bool run (bool status, int y)
Position = Rnd.Next(1,4);
p.Y = Y;
IMG.Location = p;
if(IMG.Location.X >= RaceLenght)
status = false;
Ok. This is going to be a bit difficult to explain, so please bear with me.
First off, we have to move away from your task, into more general Windows type things.
Windows operates differently from "normal" programs such as you might have written before, where flow passes from line to line, and always happens the same way.
In windows nothing happens at all unless an Event occurs to cause it to do somthing in response. That could be the user pressing a button, or a keyboard button, or some other application wanting something done for it.
When that happens, your code it told what event it is, and an event handler is executed to deal with it. If it is a button press, then you might want to close your application, or open a file. But you don't do either until the appropriate event handler is called for you.
That probably sounds irrelevant to your little dog race! :laugh:
But it isn't. Not at all. Your code has a "run" method, which loops round until the dog reaches the "finish line" - which means that nothing else happens while you are still executing the run method (that's an over simplification, but it'll do for a few weeks). So when you call "run" on one dog, it just loops round until the dog finishes, and then continues with the next dog.
So, what to do?
Firstly, I want you to forget you ever saw the Thread.Sleep method. OK? Gone? Good - you don't want it now, you don't need it now, and it is very, very likely that you will never, ever need to use it unless your application is very badly designed! So forget it! OK?
What you want to do is move a picture of a dog a random amount forward then move the others, and see if any of them have "won". Which means you need to do it in "human" time, rather than computer time - the race should probably take a couple of seconds, maybe a minute.
To do that, you want an Event which will happen at regular intervals - we call that a Timer Tick event.
So, add a private field to your form class:
private Timer raceTimer = new Timer();
Now, in your Form Load event, add the following code:
raceTimer.Interval = 100;
raceTimer.Tick += new EventHandler(raceTimer_Tick);
That builds a Timer which will call an event every tenth of a second (the 100 is in thousands of a second).
Now create the event handler:
void raceTimer_Tick(object sender, EventArgs e)
Every tenth of a second, that method will be called - so change your Dog.Run method and make it do one iteration, one random "step" forward.
Then in the Tick handler call Run for each Dog, then check if any have finished. If they have, you have a winner and can stop the race by calling raceTime.Stop()
Sounds complex? It isn't really - try it, and you will see what I mean.