当前上下文 c# 中不存在名称 sw?

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

我对编程很陌生。只做了大约 2 个月。

我一直收到错误 "The name sw does not exist in the current context" 我在顶部声明了 "using system.IO;"。

真的不知道怎么解决。有什么想法吗?

        //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();
        }

你错过了大括号。您的代码应该类似于

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

sw 仅在 using 的上下文中有效。在 using 之后没有 {},这意味着上下文仅用于下一个语句;

    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 + ": ");

除此之外的任何代码都不再出现在 using 的上下文中。这意味着 StreamWriter 已被处置并且不再可用。要定义更多上下文,您必须将依赖于 sw 的代码放在 {}

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();
}

在这里,sw 仍在上下文中并且可以使用。我希望这是有道理的。

编辑

请注意;关于上下文,{} 很重要。我从您的代码中看到 for 循环也丢失了它。这也不会像您期望的那样工作。如果我输入5,代码会显示"Please Enter your Namen"5次,然后等待1个名字

//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 不在循环上下文中。循环将在该行执行之前完成。你需要 {}

//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();
}