Save as is taking the filename provided, but is changing to null later in the code - which is causing the file never to be created

StackOverflow https://stackoverflow.com/questions/13496716

  •  01-12-2021
  •  | 
  •  

Frage

So, I had a question last time about syntax, and now that the syntax error is fixed, I have a problem that even after my professor looked it over, he doesn't know how to fix. We went through my code line by line, and with the initial save as dialog, everything looks good and the filename/path shows up in the debugger. It passes down to the create file line, and then to the code that I had to add to make my syntax work - It then proceeds to where I'm trying to open the file to be able to use the writeline command with my random number generator - And there instead of opening the appropriate file, it becomes "null" as a value! It doesn't stop there though, it continues on to the random number generator, and rolls the required number of random numbers, but of course since the opening value showed "null" it doesn't save to file like it's supposed to. Oh, and the code that was in my textbook is what generated the first syntax error without providing a way to fix it. Here's the code, sorry if it's long/difficult to read.

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.

War es hilfreich?

Lösung 2

It makes no sense to me that your while loop for numbers sits outside the if else logic of your save file dialog. If they didn't select a file then why are you still trying to write out the random numbers to a file? Move the while loop inside your if statement.

Also as Mario points out you are using mismatched filenames from 2 different dialogs so that is the root issue of the problem but I suggest you fix both to avoid future headaches.

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.");
        }
    }

Andere Tipps

You are using openFileDialog1.Filename with File.CreateText but above you use saveFileDialog1

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top