Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8880817
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:13:46+00:00 2026-06-14T20:13:46+00:00

So, I had a question last time about syntax, and now that the syntax

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T20:13:47+00:00Added an answer on June 14, 2026 at 8:13 pm

    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.");
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok, I'm now very confused. After my last question had several people comment about
I had much luck last time I submitted a question so here goes: I
I had a question about Java Class Path variables. If I have multiple jars
Last time I had a similar question, but we figured out if initialize and
I had a question about indices on a table and I put it up
I had a question for you, something that I can't seem to find the
Last time I had to deal with Java was 2005 and I forgot almost
I have opended a question about repeated parameter-checks in public methods. The result there
In the last month, we've had a persistent problem with the Python 2.6.x multiprocessing
I have been using WCF web services for around about a year now and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.