Search code examples
c#streamwriter

the name sw does not exist in the current context c#?


I am very new to programming. Have only been doing it for around 2 months.

I keep getting the error "The name sw does not exist in the current context" i have declared "using system.IO;" at the top.

Really have no idea how to fix it. Any ideas?

        //enter new user

        if (iOption == 2)
        {
            //Enter new user information and add them to file
            Console.Write("Please Enter the number of users you wish to log: ");
            iUserNumber = Convert.ToInt32(Console.ReadLine());

            //open file for writing, in APPEND mode
            using (StreamWriter sw = new StreamWriter("NewUser.txt", true))

                //loop for each user
                for (iCount = 1; iCount <= iUserNumber; iCount++)

                    // enter details
                    Console.Write("Please Enter your Name" + iCount + ": ");
            sName = Console.ReadLine();

            // write to file
            sw.WriteLine(sName);
            Console.WriteLine();

            Console.Write("Please Enter the Name of your School: ");
            iSchoolName = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine();

            Console.Write("Please Enter the name of your class: ");
            iClassName = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine();
        }

Solution

  • sw is only valid in the context of the using. You have no {} after your using which means the context is for the next statement ONLY;

        using (StreamWriter sw = new StreamWriter("NewUser.txt", true))
    
            //loop for each user
            for (iCount = 1; iCount <= iUserNumber; iCount++)
    
                // enter details
                Console.Write("Please Enter your Name" + iCount + ": ");
    

    Any code beyond this is no longer in the context of the using. This means the StreamWriter has been disposed and is no longer available. To define more context, you have to put the code that is dependent on sw in {}

    using (StreamWriter sw = new StreamWriter("NewUser.txt", true))
    {
        //loop for each user
        for (iCount = 1; iCount <= iUserNumber; iCount++)
    
            // enter details
            Console.Write("Please Enter your Name" + iCount + ": ");
        sName = Console.ReadLine();
    
        // write to file
        sw.WriteLine(sName);
        Console.WriteLine();
    }
    

    Here, sw is still in context and available to use. I hope this makes sense.

    Edit

    Just a note; With regard to context, the {} is important. I see from your code that the for loop is missing it, too. This will not work as you expect either. If I entered 5, the code will display "Please Enter your Namen" 5 times, and then wait for 1 name.

    //loop for each user
    for (iCount = 1; iCount <= iUserNumber; iCount++)
        // enter details
        Console.Write("Please Enter your Name" + iCount + ": ");
    sName = Console.ReadLine();
    
    // write to file
    sw.WriteLine(sName);
    Console.WriteLine();
    

    sname is not in context of the loop. The loop will complete before that line is executed. You need {}

    //loop for each user
    for (iCount = 1; iCount <= iUserNumber; iCount++)
    {
        // enter details
        Console.Write("Please Enter your Name" + iCount + ": ");
        sName = Console.ReadLine();
    
        // write to file
        sw.WriteLine(sName);
        Console.WriteLine();
    }