Click here to Skip to main content
15,886,046 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
hi,

im havin problems creating a class for first time. The programm is about a dog race, so if you need more explanaitions tell me please.

Here is my class:


C#
class Dog
{
   public int RaceLenght =20;
   public int StartPosition = 0;
   public PictureBox IMG;
   public int Position;
   public Random Rnd;

   public bool Run(bool status)
   {
      return status;
   }

   public void reset()
   {
      Position = StartPosition;
   }
}



Here is my form:

C#
string[]Arr = new string[4];
Dog Dog1 = new Dog();
Dog Dog2 = new Dog();
Dog Dog3 = new Dog();
Dog Dog4 = new Dog();
int distanz1;
int distanz2;
int distanz3;
int distanz4;


Run Button:


C#
Dog1.Run(true)
Dog1.IMG = picturebox1;
Point p = picturebox1.Location;
distanz1 = Dog1.Rnd.Next(1,4);
p.X += distanz1;
Img.Location = p;


now i would do this for every object, what stands in the button, for dog2,3 and 4.

i think my code isnt the very best..
Posted
Updated 15-Oct-12 5:34am
v3
Comments
Herman<T>.Instance 15-Oct-12 7:31am    
what is your exact problem?
do you get exceptions?
niko_tells 15-Oct-12 7:34am    
yes, out of bound at run button: distanz1 = dog1.rnd.Next(1,4) saying that i didnt create an new objekt. But i have -> Dog Dog1 = new Dog(); Also i would prefere dont writing the same for every dog in the runbutton, i would prefere this way more: run button: Dog1.Run(true); Dog2.Run(true);...
n.podbielski 15-Oct-12 7:47am    
Did you assigned value to Dog.Rnd at some point? For me this looks like null.
Herman<T>.Instance 15-Oct-12 7:49am    
4 dogs so dog[0] till dog[3] so when the randomizer says dog[4] you have a problem

1 solution

You haven't created an instance of the Random class within your Dog class:
C#
class Dog
{
public int RaceLenght =20;
public int StartPosition = 0;
public PictureBox IMG;
public int Position;
public Random Rnd;
You could add the new bit:
C#
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 static random class and use that instead for all instances:
C#
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:

..
C#
public Point p = new Point();

public bool run (bool status, int y) //y = y pixels position of images
{
while (status)
{
Position = Rnd.Next(1,4);
p.X +=Position;
p.Y = Y;
IMG.Location = p;

if(IMG.Location.X >= RaceLenght)
{
status = false;
}
Thread.Sleep(10);


}
return status;
}
"




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:
C#
private Timer raceTimer = new Timer();

Now, in your Form Load event, add the following code:
C#
raceTimer.Interval = 100;
raceTimer.Tick += new EventHandler(raceTimer_Tick);
raceTimer.Start();
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:
C#
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.
 
Share this answer
 
v2
Comments
niko_tells 15-Oct-12 10:13am    
ok i have solved it on my own, but now the problem is, that dog1 runs first, afterwards dog2 runs when dog1 finished running etc.. until dog 4 ends. how do i initialize 4 objekts at the same time?
OriginalGriff 15-Oct-12 10:27am    
Depends what you are trying to do, and how you are doing it.
I don't want to explain something that *sounds* like it will solve your problem if it is a concept you have not got to yet, and might well confuse.

Why don't you explain what task you are trying to perform, and how you are trying to do it? Then I'll see if I can help at all.
niko_tells 15-Oct-12 10:34am    
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) //y = y pixels position of images
{
while (status)
{
Position = Rnd.Next(1,4);
p.X +=Position;
p.Y = Y;
IMG.Location = p;

if(IMG.Location.X >= RaceLenght)
{
status = false;
}
Thread.Sleep(10);


}
return status;
}
OriginalGriff 15-Oct-12 11:04am    
Answer updated
Monjurul Habib 15-Oct-12 16:39pm    
5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900