【问题标题】:Can't declare an object in if else. Not visible outside if else clause无法在 if else 中声明对象。在 if else 子句之外不可见
【发布时间】:2013-11-09 08:26:32
【问题描述】:

当我在 if/else 子句中实例化 Textwriter 对象时,它在子句之外是不可见的。我可以在这里做什么?我想追加或写入一个新文件。

代码如下:

     private static void eventfull_doing()
    {
        string path, line;
        int countLines;
        Console.Write("Enter the name(with extension) of the file in the \"data\" folder, or create a new name(with extension): ");
        path = Console.ReadLine();
        TextReader inFile = new StreamReader(@"C:\data\" + path);
        Console.Write("How many lines of text do you want to add to the file?: ");
        countLines = Convert.ToInt32(Console.ReadLine());
        if (inFile.Peek() == -1 || inFile == null)
        {
            TextWriter outFile = File.AppendText(@"C:\data\" + path);
        }
        else
        {
            TextWriter outFile = new StreamWriter(@"C:\data\" + path);
        }
        for (int i = 0; i < countLines; i++)
        {
            Console.Write("Enter line: ");
            line = Console.ReadLine();
            outFile.
    }

这是我所做的:请注意,在第一个 sn-p 中,if 条件不是预期的。

    string path, line;
        int countLines;
        Console.Write("Enter the name(with extension) of the file in the \"data\" folder, or create a new name(with extension): ");
        path = Console.ReadLine();
        string file = Path.Combine(@"c:\data", path);
        Console.Write("How many lines of text do you want to add to the file?: ");
        countLines = Convert.ToInt32(Console.ReadLine());
        TextWriter outFile = null;
        if (File.Exists(file))
        {
            using (outFile = File.AppendText(file))
            {
                for (int i = 0; i < countLines; i++)
                {
                    Console.Write("Enter line: ");
                    line = Console.ReadLine();
                    outFile.WriteLine(line);
                }
            }
        }
        else
        {
            using (outFile = new StreamWriter(file))
            {
                for (int i = 0; i < countLines; i++)
                {
                    Console.Write("Enter line: ");
                    line = Console.ReadLine();
                    outFile.WriteLine(line);
                }
            }
        }

    }

【问题讨论】:

  • 正常。在 if else 语句之外实例化它。

标签: c# object if-statement declare


【解决方案1】:

您可以在 if 语句之前声明它,但不进行赋值 - 然后确保在两个分支中都为其赋值:

TextWriter outFile;
if (inFile.Peek() == -1 || inFile == null)
{
    outFile = File.AppendText(@"C:\data\" + path);
}
else
{
    outFile = new StreamWriter(@"C:\data\" + path);
}

但是,如果文件存在,您仍然打开文件这一事实很可能会给您带来问题 - 如果您发现它仍然是空的,则不清楚为什么要调用 File.AppendText。你实际上只是想创建或追加?如果是这样,只需使用AppendText - 就可以了。

您还应该使用using 语句自动关闭编写器...如果您确实需要在多个地方使用@"C:\data\" + path,我会将该常用表达式提取为局部变量:

string file = Path.Combine(@"c:\data", path);
// Now use file everywhere

如果您确实坚持使用File.AppendTextStreamWriter 构造函数,请考虑使用条件表达式:

TextWriter outFile = inFile.Peek() == -1 || inFile == null
    ? File.AppendText(file) : new StreamWriter(file);

【讨论】:

    【解决方案2】:

    这是设计使然。你需要这样写:

    TextWriter outFile = null; 
    if (inFile.Peek() == -1 || inFile == null)
    {
        outFile = File.AppendText(@"C:\data\" + path);
    }
    else
    {
        outFile = new StreamWriter(@"C:\data\" + path);
    }
    

    在这种情况下,将null 分配给 outfile 是可选的,但是当您开始使用 C++ 进行编码时,您会发现这是一个很好的做法。

    【讨论】:

    • 我完全不同意 null 是好的做法。这在 C# 中是一种多余的干扰,应该避免。在学习 C++ 时,与指针、管理自己的内存分配等相比,初始化为 null 确实是一个小细节。
    • 如果您后来发现根本不需要 else 案例怎么办?好的,在 C# 中,编译器会抱怨一个可能未初始化的变量,但这至少会让你花费另一个编译器运行。但是如果你的代码库混合了 C# 和 C/C++,你很快就会遇到麻烦。
    【解决方案3】:

    请在外面声明它并在里面实例化它, 这是正常行为,如果您在代码块中声明一个仅在该代码块中可用的变量,请在代码块之外声明该变量并在您的 if else 代码块中实例化或为其分配一些东西

    错误

    if ( condition ) 
    {
     string s = "J" ;  
    }
    MessageBox.Show(s) 
    

    正确

    string s ; 
    if ( condition ) 
    { 
      s = "jo";
    } 
    MessageBox.Show(s) ;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-12-03
      • 1970-01-01
      • 2012-04-21
      • 2014-04-12
      • 1970-01-01
      • 2019-04-09
      • 1970-01-01
      相关资源
      最近更新 更多