Waseem Abbas
Waseem Abbas

Reputation: 5

The name "......." does not exist in the current context. error

I am using a book to learn C# in which I was asked to type the following code but for InFile and OutFile used in the code it says:

The name "InFile" does not exist in the current context. and The name "OutFile" does not exist in the current context.

Code is given below:

using System;
using System.IO;

class NumberIt
{
    public static void Main(string[] args)
    {
        if (args.Length <= 0)
        {
            Console.WriteLine("\nYou need to include a filename.");
        }
        else
        {
            StreamReader InFile = null;
            StreamWriter OutFile = null;
        }
        try
        {
            InFile = File.OpenText(args[0]);
            OutFile = File.CreateText("OutFile.txt");
            Console.Write("\nNumbering...");
            string line = InFile.ReadLine();
            int ctr = 1;

        while (line != null)
        {
            OutFile.WriteLine("{0}:{1}", ctr.ToString().PadLeft(3, '0'), line);
            Console.Write("..{0}..", ctr.ToString());
            ctr++;
            line = InFile.ReadLine();
        }
    }
    catch (System.IO.FileNotFoundException)
    {
        Console.WriteLine("Could not find the file {0}", args[0]);
    }
    catch (Exception e)
    {
        Console.WriteLine("Error: {0}", e.Message); 
    }
    finally
    {
        if (InFile != null)
        {
            InFile.Close();
            OutFile.Close();
            Console.WriteLine("...Done");
        }
    }
}
}

Upvotes: 0

Views: 3333

Answers (3)

alzaimar
alzaimar

Reputation: 4622

The variables are declared, but within a different scope. A 'scope' is -generally speaking- code within curly braces.

A declaration of variables is not a statement, so you cannot read your code like 'if the arguments are ok, then declare the two variables'. A variable declaration should be read like 'Here are two containers of type XY which will be known until the scope ends..'.

So your code should look similar to this.

public static void Main(string[] args)
{
    if (args.Length <= 0)
    {
        Console.WriteLine("\nYou need to include a filename.");
    }
    else
    {
       StreamReader InFile = null;
       StreamWriter OutFile = null;

       try
       {
           InFile = File.OpenText(args[0]);
           OutFile = File.CreateText("OutFile.txt");
           Console.Write("\nNumbering...");
       ...
       }
       catch ... 
       {
       }
    // InFile and OutFile still known here !
    }
 // InFile and OutFile are unknown here !

Hope this describes what others already mentioned.

Upvotes: 1

Mattias &#197;slund
Mattias &#197;slund

Reputation: 3907

The variables InFile and OutFile are out of scope for the try/catch block.

Move the try/catch block inside the else statement above it, so the variables are still available. Easiest is to move the } above try to the very end.

Upvotes: 0

Selman Gen&#231;
Selman Gen&#231;

Reputation: 101681

you should declare InFile and Outfile in the scope of your Main method, or outside of the Main.

class NumberIt
{
    // declare them here
    public static void Main(string[] args)
    { 
       // or here
       ... 
    }
}

Upvotes: 0

Related Questions