Search code examples
c#objectif-statementdeclare

Can't declare an object in if else. Not visible outside if else clause


When I instantiate the Textwriter object in the if/else clause, it is not visible outside the clause. What can I do here? I want to either append to or write a new file.

Here is the code:

     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.
    }

Here is what I've done: Notice that in the first snippet, the if condition is not what was intended.

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

    }

Solution

  • You can declare it before the if statement, but with no assignment - and then make sure that you assign a value to it in both branches:

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

    However, the fact that you still have the file open if it exists may well cause you problems - and it's not clear why you're calling File.AppendText if you've found that it's empty anyway. Are you actually just trying to create or append? If so, just use AppendText - it'll be fine.

    You should also use a using statement to close the writer automatically... and if you do need to use @"C:\data\" + path in several places, I'd extract that common expression to a local variable:

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

    If you do stick to using File.AppendText or the StreamWriter constructor, consider using a conditional expression:

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