ということで、前回シンタックスについて質問がありましたが、シンタックスエラーが修正された今、教授が見直しても修正方法がわからないという問題があります。コードを 1 行ずつ調べたところ、最初の [名前を付けて保存] ダイアログですべてが正常に表示され、デバッガーにファイル名/パスが表示されました。それはファイルの作成行に渡され、次に構文を機能させるために追加する必要があったコードに渡されます-次に、ファイルを開こうとしている場所に進み、乱数で writeline コマンドを使用できるようにしますジェネレーター - そして、適切なファイルを開く代わりに、値として「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();
}
I included just the parts that are pertinent - I do have a little bit after the fact but that's just for the exit / clear buttons and the debugger doesn't jump to them at all so i clipped out the superfluous.