所以,我上次有一个关于语法的问题,现在语法错误已修复,我有一个问题,即使在我的教授看了之后,他也不知道如何修复。我们逐行完成了我的代码,并使用初始保存为对话框,一切看起来都很好,文件名/路径显示在调试器中。它传递给创建文件行,然后传递给我必须添加的代码以使我的语法工作 - 然后它继续到我试图打开文件的位置,以便能够使用带有随机数的writeline命令generator - 而不是打开相应的文件,它变为“null”作为值!它不会停在那里,它继续到随机数生成器,并滚动所需数量的随机数,但当然因为开放值显示“null”它不会像它应该保存到文件。哦,我教科书中的代码是生成第一个语法错误而没有提供修复方法的原因。这是代码,对不起,如果它很长/很难阅读。
using System.IO; // Added to be able to use StreamWriter variable type
namespace Random_Number_File_Writer
{
public partial class Form1 : Form
{
StreamWriter randomNumberFile; //Name streamwriter
decimal numbers; //Variable to insert the number up down value into
Random rand1 = new Random(); //Random number generator
int writeitem; // Variable to insert random number into, to write.
public Form1()
{
InitializeComponent();
}
public void saveFileDialog1_FileOk(object sender, CancelEventArgs e)
{
}
private void generateButton_Click(object sender, EventArgs e)
{
try
{
//Initial opening point for save file dialogue
saveFileDialog1.InitialDirectory = @"C:\Users\Heather\Documents\Visual Studio 2010\Projects\Random Number File Writer";
//Save As popup - Opening the file for writing usage once it's created.
if(saveFileDialog1.ShowDialog() == DialogResult.OK)
{
randomNumberFile = File.CreateText(openFileDialog1.FileName);
}
else // Popup informing user that the data will not save to a file because they didn't save.
{
MessageBox.Show("You elected not to save your data.");
}
numbers = numericUpDown1.Value; //Gathering the number of numbers to generate from the number box.
while (numbers > 0) // Loop counting down to 0 to give the user the appropriate number of requested random numbers.
{
writeitem = rand1.Next(101); // Random number generated.
randomNumberFile.WriteLine(writeitem); //Random number written to file
numbers--; // Initial number for user input decremented so that loop will have an ending and user only gets the amount of randoms asked for.
}
randomNumberFile.Close();
}
我只包含了相关的部分 - 事后我确实有一点点,但这只是针对退出/清除按钮而且调试器根本没有跳转到它们,所以我剪掉了多余的部分。
答案 0 :(得分:4)
您正在openFileDialog1.Filename
使用File.CreateText
,但使用上方saveFileDialog1
答案 1 :(得分:0)
对我而言,数字的while循环位于保存文件对话框的if else逻辑之外是没有意义的。如果他们没有选择文件,那么为什么还要尝试将随机数写入文件?在if语句中移动while循环。
此外,正如Mario所指出的那样,您使用的是来自2个不同对话框的不匹配文件名,因此这是问题的根本问题,但我建议您解决这两个问题以避免将来出现问题。
try
{
//Initial opening point for save file dialogue
saveFileDialog1.InitialDirectory = @"C:\Users\Heather\Documents\Visual Studio 2010\Projects\Random Number File Writer";
//Save As popup - Opening the file for writing usage once it's created.
if(saveFileDialog1.ShowDialog() == DialogResult.OK)
{
randomNumberFile = File.CreateText(saveFileDialog1.FileName);
numbers = numericUpDown1.Value; //Gathering the number of numbers to generate from the number box.
while (numbers > 0) // Loop counting down to 0 to give the user the appropriate number of requested random numbers.
{
writeitem = rand1.Next(101); // Random number generated.
randomNumberFile.WriteLine(writeitem); //Random number written to file
numbers--; // Initial number for user input decremented so that loop will have an ending and user only gets the amount of randoms asked for.
randomNumberFile.Close();
}
}
else // Popup informing user that the data will not save to a file because they didn't save.
{
MessageBox.Show("You elected not to save your data.");
}
}