Program crashes after a few executions

  • Thread starter Thread starter Thomas Neubauer
  • Start date Start date
T

Thomas Neubauer

Hello,

i am learning c# and have created now a simple project that just creates 6
random numbers.
My form includes a button and 6 labels for the random numbers. The program
seems to work correct,
however when after a few button clicks the program crashes.

Does anyone know where the reason could be?

--
Kind regards
Thomas Neubauer



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void btZieheLottozahlen_Click(object sender, EventArgs e)
{
int[] Zahlen = new int[6];
Random Zufallszahl = new Random();
int Doppelte = 0;

do
{
for (int i = 0; i < 6; i++)
{
Zahlen = Zufallszahl.Next(1, 64);
}
Array.Sort(Zahlen);
for (int i = 0; i < 5; i++)
if (Zahlen == Zahlen[i + 1])
Doppelte = 1;

} while (Doppelte == 1);

label1.Text = Convert.ToString(Zahlen[0]);
label2.Text = Convert.ToString(Zahlen[1]);
label3.Text = Convert.ToString(Zahlen[2]);
label4.Text = Convert.ToString(Zahlen[3]);
label5.Text = Convert.ToString(Zahlen[4]);
label6.Text = Convert.ToString(Zahlen[5]);
}
}
}
 
Thomas Neubauer said:
i am learning c# and have created now a simple project that just creates 6
random numbers.
My form includes a button and 6 labels for the random numbers. The program
seems to work correct,
however when after a few button clicks the program crashes.

Does anyone know where the reason could be?

Well, in what way does it crash? Is there an exception?
 
Hello,

i am learning c# and have created now a simple project that just
creates 6 random numbers.
My form includes a button and 6 labels for the random numbers. The
program seems to work correct,
however when after a few button clicks the program crashes.

Does anyone know where the reason could be?

I don't see anything obvious in the code you posted that would cause a crash.

Which is not to say the code is bug-free:

* I wouldn't send an int in to do a bool's job, but that problem
shouldn't cause your program to crash.

* Perhaps more distressingly, you never reset "Doppelte" in your
loop, so if you ever fail to have an array of unique random numbers,
your loop will never exit.

The latter problem would cause your program to stop responding, but a)
I wouldn't call that a "crash", and b) if you interrupt the program in
the debugger you would easily see where the program is stuck, and you
could also just as easily step through the code to see what happens to
the variables and hopefully notice when one isn't behaving as you
expect it to.

The debugger is there to help you. You should use it. :)

Still, you haven't really been very specific in your post. If the
above information doesn't help, what does "crash" mean? Are you
getting an exception? If so, what is the exception? On what line of
code does it occur? What other code is part of your application?

Pete
 
You don't reset Doppelte to 0 at the start of a loop; once you have
hit a duplicate it will loop forever.

Marc
 
I love it!

Peter Duniho said:
I don't see anything obvious in the code you posted that would cause a crash.

Which is not to say the code is bug-free:

* I wouldn't send an int in to do a bool's job, but that problem
shouldn't cause your program to crash.

* Perhaps more distressingly, you never reset "Doppelte" in your
loop, so if you ever fail to have an array of unique random numbers,
your loop will never exit.

The latter problem would cause your program to stop responding, but a)
I wouldn't call that a "crash", and b) if you interrupt the program in
the debugger you would easily see where the program is stuck, and you
could also just as easily step through the code to see what happens to
the variables and hopefully notice when one isn't behaving as you
expect it to.

The debugger is there to help you. You should use it. :)

Still, you haven't really been very specific in your post. If the
above information doesn't help, what does "crash" mean? Are you
getting an exception? If so, what is the exception? On what line of
code does it occur? What other code is part of your application?

Pete
 
Sorry, I never even saw the code, and thought you were making up terms like
Doppelte... Long day at the office...
 
Sorry, I never even saw the code, and thought you were making up terms like
Doppelte... Long day at the office...

lol...I was wondering what I'd written that made you so happy.

Still, sounds like something funny to do some day. Maybe next April 1,
we should just all answer questions by sprinkling in made-up words
throughout. :)
 
Hello all,

thanks to all for your very fast help. I haven't expected such fast help, so
i was really surprised.
I meant that my program doesn't react anymore after a few clicks. Probably
"crash" was the wrong word.
Anyway i solved through setting variable "Doppelte" to 0 on the test of the
last array element.

do
{
for (int i = 0; i < 6; i++)
{
Zahlen = Zufallszahl.Next(1, 64);
}
Array.Sort(Zahlen);
for (int i = 0; i < 5; i++)
if (Zahlen == Zahlen[i + 1])
Doppelte = 1;
else
if (Zahlen == 4)
Doppelte = 0;

} while (Doppelte == 1);
 
Hello all,

thanks to all for your very fast help. I haven't expected such fast
help, so i was really surprised.
I meant that my program doesn't react anymore after a few clicks.
Probably "crash" was the wrong word.
Anyway i solved through setting variable "Doppelte" to 0 on the test of
the last array element.

Yuck!

Why not just set it in the first line of the do/while loop? There's
not even a need to initialize in the declaration if you do that.

I would still change it to a bool, in any case.

Pete
 
Ditto that "Yuck!"

Actually you could make the whole thing a lot simpler... it looks like
you just want 6 sorted random numbers in a range with no duplicates...
so how about:

Random rand = new Random();
List<int> found = new List<int>();
while (found.Count < 6) {
int newNumber;
while (found.Contains(newNumber = rand.Next(1, 64)))
{ }
found.Add(newNumber);
}
found.Sort();
int[] array = found.ToArray(); // if you *need* an array

One advantage here is that it doesn't throw away everything just
because of a collision - it just dumps the duplicated value; this
might be significant if the numbers get larger. But more importantly
there are no complicated exit variables to set/reset - just "have we
got everything we need yet?"

Marc
 
And in true style I *still* over-complicated things!

Following is simpler and avoids nested loop;

while (found.Count < 6) {
int newNumber = rand.Next(1, 64);
if (!found.Contains(newNumber)) {
found.Add(newNumber);
}
}
 
Hi,

thanks to all for your excellent help!

BTW: Doppelte is a germany word. I'm sorry, but it was just one of my first
sample projects,
but of course, normally all my codes are in english.
 
-----Original Message-----
From: Marc Gravell [mailto:[email protected]]
Posted At: Wednesday, 21 November 2007 2:32 PM
Posted To: microsoft.public.dotnet.languages.csharp
Conversation: Program crashes after a few executions
Subject: Re: Program crashes after a few executions

And in true style I *still* over-complicated things!

Following is simpler and avoids nested loop;

I must admit I was wondering why you had done it the first way, it
seemed overly complicated ;)
 
-----Original Message-----
From: Thomas Neubauer [mailto:[email protected]]
Posted At: Thursday, 22 November 2007 8:04 AM
Posted To: microsoft.public.dotnet.languages.csharp
Conversation: Program crashes after a few executions
Subject: Re: Program crashes after a few executions

Hi,

thanks to all for your excellent help!

BTW: Doppelte is a germany word. I'm sorry, but it was just one of my
first
sample projects,
but of course, normally all my codes are in english.

It (Doppelte) means Doubled if I recall correctly. My German is a bit
rusty though.
 
Back
Top