Visual C# 2010 - Application Crashes At A Certain Time (Causing VS2010 To Crash As Well)
-
Hi, I'm making a Bingo Generator for a assignment, but it generates the last number, then should you click the button again it crashes, also crashing VS2010 so I don't get a error either. here's the button code:
var lineCount = 1;
using (var reader = File.OpenText(@"saved.bingo"))
{
while (reader.ReadLine() != null)
{
lineCount++;
}
}TextWriter saveCalled = new StreamWriter("saved.bingo"); saveCalled.Write(called.Text); saveCalled.Close(); TextReader loadSaved = new StreamReader("saved.bingo"); string saved = loadSaved.ReadToEnd(); loadSaved.Close(); randomGenerated.generate(); TextReader nextBingo = new StreamReader("bingoNext.bingo"); string bingoNum = nextBingo.ReadToEnd(); nextBingo.Close(); bingoNum = bingoNum.Trim(); if (saved.Contains(bingoNum) == true) { randomGenerated.generate(); } else { TextReader tr = new StreamReader("saved.bingo"); string saveNm = tr.ReadToEnd(); tr.Close(); int saveNum = saveNm.Count(); if (saveNum >= 15) { button1.Visible = false; MessageBox.Show("Game Over."); } newnum.Text = bingoNum; TextReader nextCalled = new StreamReader("calledNext.bingo"); string calledNum = nextCalled.ReadToEnd(); nextCalled.Close(); called.Text = calledNum; }
and the generator code:
namespace openBingo
{
public class randomGenerated
{
public static void generate()
{
System.Random linegrab = new System.Random(DateTime.Now.Millisecond);TextReader numRead = new StreamReader("numbers.bingo"); ArrayList lines = new ArrayList(); string line; while ((line = numRead.ReadLine()) != null) { lines.Add(line); } int randomIndex = linegrab.Next(lines.Count); string bingoNum = (lines
-
Hi, I'm making a Bingo Generator for a assignment, but it generates the last number, then should you click the button again it crashes, also crashing VS2010 so I don't get a error either. here's the button code:
var lineCount = 1;
using (var reader = File.OpenText(@"saved.bingo"))
{
while (reader.ReadLine() != null)
{
lineCount++;
}
}TextWriter saveCalled = new StreamWriter("saved.bingo"); saveCalled.Write(called.Text); saveCalled.Close(); TextReader loadSaved = new StreamReader("saved.bingo"); string saved = loadSaved.ReadToEnd(); loadSaved.Close(); randomGenerated.generate(); TextReader nextBingo = new StreamReader("bingoNext.bingo"); string bingoNum = nextBingo.ReadToEnd(); nextBingo.Close(); bingoNum = bingoNum.Trim(); if (saved.Contains(bingoNum) == true) { randomGenerated.generate(); } else { TextReader tr = new StreamReader("saved.bingo"); string saveNm = tr.ReadToEnd(); tr.Close(); int saveNum = saveNm.Count(); if (saveNum >= 15) { button1.Visible = false; MessageBox.Show("Game Over."); } newnum.Text = bingoNum; TextReader nextCalled = new StreamReader("calledNext.bingo"); string calledNum = nextCalled.ReadToEnd(); nextCalled.Close(); called.Text = calledNum; }
and the generator code:
namespace openBingo
{
public class randomGenerated
{
public static void generate()
{
System.Random linegrab = new System.Random(DateTime.Now.Millisecond);TextReader numRead = new StreamReader("numbers.bingo"); ArrayList lines = new ArrayList(); string line; while ((line = numRead.ReadLine()) != null) { lines.Add(line); } int randomIndex = linegrab.Next(lines.Count); string bingoNum = (lines
Wrap the code in
try/catch
block and print out the exception.Best wishes, Navaneeth
-
Hi, I'm making a Bingo Generator for a assignment, but it generates the last number, then should you click the button again it crashes, also crashing VS2010 so I don't get a error either. here's the button code:
var lineCount = 1;
using (var reader = File.OpenText(@"saved.bingo"))
{
while (reader.ReadLine() != null)
{
lineCount++;
}
}TextWriter saveCalled = new StreamWriter("saved.bingo"); saveCalled.Write(called.Text); saveCalled.Close(); TextReader loadSaved = new StreamReader("saved.bingo"); string saved = loadSaved.ReadToEnd(); loadSaved.Close(); randomGenerated.generate(); TextReader nextBingo = new StreamReader("bingoNext.bingo"); string bingoNum = nextBingo.ReadToEnd(); nextBingo.Close(); bingoNum = bingoNum.Trim(); if (saved.Contains(bingoNum) == true) { randomGenerated.generate(); } else { TextReader tr = new StreamReader("saved.bingo"); string saveNm = tr.ReadToEnd(); tr.Close(); int saveNum = saveNm.Count(); if (saveNum >= 15) { button1.Visible = false; MessageBox.Show("Game Over."); } newnum.Text = bingoNum; TextReader nextCalled = new StreamReader("calledNext.bingo"); string calledNum = nextCalled.ReadToEnd(); nextCalled.Close(); called.Text = calledNum; }
and the generator code:
namespace openBingo
{
public class randomGenerated
{
public static void generate()
{
System.Random linegrab = new System.Random(DateTime.Now.Millisecond);TextReader numRead = new StreamReader("numbers.bingo"); ArrayList lines = new ArrayList(); string line; while ((line = numRead.ReadLine()) != null) { lines.Add(line); } int randomIndex = linegrab.Next(lines.Count); string bingoNum = (lines
Hi, you need to figure out where things go wrong. Start by using a try-catch block, and displaying the exception, all of it. Hence use something along these lines:
try {
// existing code goes here
} catch (Exception exc) {
Console.WriteLine(exc.ToString());
}You can do this in the button click handler, in the generate() method, etc. Then watch the line numbers (and tell Visual to always show them, see here[^]) BTW: your program looks terrible. Many things seem less than good. - Why are you writing and reading files all the time? Don't you trust the computer's memory? FYI: there are File methods that read or write all text of a file. - For random numbers, you should only have a single RNG and call Next on it, instead of creating new ones. - generate() is recursive. Any idea what keeps it from recursing forever? (which would cause a StackOverflowException). - and please stop abusing the
var
keyword, use real types instead. :)Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
-
Hi, you need to figure out where things go wrong. Start by using a try-catch block, and displaying the exception, all of it. Hence use something along these lines:
try {
// existing code goes here
} catch (Exception exc) {
Console.WriteLine(exc.ToString());
}You can do this in the button click handler, in the generate() method, etc. Then watch the line numbers (and tell Visual to always show them, see here[^]) BTW: your program looks terrible. Many things seem less than good. - Why are you writing and reading files all the time? Don't you trust the computer's memory? FYI: there are File methods that read or write all text of a file. - For random numbers, you should only have a single RNG and call Next on it, instead of creating new ones. - generate() is recursive. Any idea what keeps it from recursing forever? (which would cause a StackOverflowException). - and please stop abusing the
var
keyword, use real types instead. :)Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
-
Normally you keep the data in variables as long as your app is running; and you save it to disk, if needed, when the app exits. :)
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
-
Normally you keep the data in variables as long as your app is running; and you save it to disk, if needed, when the app exits. :)
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
-
Haha, I do try, but sometimes i need to use the data of the variable in a diffrent button control or something, and it wont let it. I tried settting it to something like:
public void button1
but no luck :( Thanks, Ben. -
Hi, I'm making a Bingo Generator for a assignment, but it generates the last number, then should you click the button again it crashes, also crashing VS2010 so I don't get a error either. here's the button code:
var lineCount = 1;
using (var reader = File.OpenText(@"saved.bingo"))
{
while (reader.ReadLine() != null)
{
lineCount++;
}
}TextWriter saveCalled = new StreamWriter("saved.bingo"); saveCalled.Write(called.Text); saveCalled.Close(); TextReader loadSaved = new StreamReader("saved.bingo"); string saved = loadSaved.ReadToEnd(); loadSaved.Close(); randomGenerated.generate(); TextReader nextBingo = new StreamReader("bingoNext.bingo"); string bingoNum = nextBingo.ReadToEnd(); nextBingo.Close(); bingoNum = bingoNum.Trim(); if (saved.Contains(bingoNum) == true) { randomGenerated.generate(); } else { TextReader tr = new StreamReader("saved.bingo"); string saveNm = tr.ReadToEnd(); tr.Close(); int saveNum = saveNm.Count(); if (saveNum >= 15) { button1.Visible = false; MessageBox.Show("Game Over."); } newnum.Text = bingoNum; TextReader nextCalled = new StreamReader("calledNext.bingo"); string calledNum = nextCalled.ReadToEnd(); nextCalled.Close(); called.Text = calledNum; }
and the generator code:
namespace openBingo
{
public class randomGenerated
{
public static void generate()
{
System.Random linegrab = new System.Random(DateTime.Now.Millisecond);TextReader numRead = new StreamReader("numbers.bingo"); ArrayList lines = new ArrayList(); string line; while ((line = numRead.ReadLine()) != null) { lines.Add(line); } int randomIndex = linegrab.Next(lines.Count); string bingoNum = (lines
Ben Magee wrote:
it generates the last number
Do you mean it goes through all the available numbers and then you try to get one more? When there aren't any more? Don't you want to reset at some point?
-
Ben Magee wrote:
it generates the last number
Do you mean it goes through all the available numbers and then you try to get one more? When there aren't any more? Don't you want to reset at some point?
When the form loads, it generates all the numbers, and writes them to a file. When you press the button, it gets a random line from that file, and then the number on it, it then checks if the number is in a file with the numbers already called, if it isnt, it adds it to that file and then displays it. Forgive me, I am still relatively new to C# - and I am looking up on sharing variables between classes now. BtM.
-
Ben Magee wrote:
I tried settting it to something like: public void button1 but no luckFrown
I think maybe you need to study the basics of C#.
-
Richard MacCutchan wrote:
I think maybe you need to study the basics of C#.
Did some studying, I now understand I need a public class with static variables inside to store data. Thanks for the advice (: Ben.
-
When the form loads, it generates all the numbers, and writes them to a file. When you press the button, it gets a random line from that file, and then the number on it, it then checks if the number is in a file with the numbers already called, if it isnt, it adds it to that file and then displays it. Forgive me, I am still relatively new to C# - and I am looking up on sharing variables between classes now. BtM.
Regardless of the implementation; once you've selected all the numbers, there are no more to select no matter how hard you try. You need to reset.