locked
does not exist in the current context problem...

    Question

  • Hi, does anyone know why I get this error?

     

    The code goes...

     

    class FileReaderClass

    {

     

    static void Main(string[] args)

    {

    try

    {

    FileReaderClass myClass = new FileReaderClass();

    }

    catch(Exception Ex)

    {

    Console.WriteLine(Ex.ToString());

    }

     

    ...

     

    foreach (string file in fileArray)

    {

    myClass.totals(file, targetDir);

    }

     

    ...

     

     

    private void totals(string fileName,string targetDir)

    {

    }

     

    }

     

    }

     

    Error 1 The name 'myClass' does not exist in the current context C:\DemoProjects\dotnet2filethingy\dotnet2filethingy\Program.cs 82 17 FileReader

    Obviously I have missed out some of the fucntional bulk out of my listing but when It compiles I have an error that the clas does not exist in its current context. When I take it out of the try block it works...

     

    Anyone know why this is?

     

    Cheers

    Pete

     

     

    Tuesday, July 31, 2007 11:24 AM

Answers

  •  

    Oh my oh my oh my the amount of times i've fallen stray to that little error Big Smile

     

    Basically I'll bet money yuo added the try later.

     

    Your variable myClass has fallen out of scope when the try block finishes.

     

    so change to this.

     

    FileReaderClass myClass = null;

    try

    {

    myClass = new FileReaderClass();

    }

    catch(Exception Ex)

    {

    Console.WriteLine(Ex.ToString());

    }

     

    ...

     

    foreach (string file in fileArray)

    {

    myClass.totals(file, targetDir);

    }

     

     

    And curse youself for you shall make that mistake 100 times before it sinks in Big Smile

    Tuesday, July 31, 2007 11:33 AM

All replies

  •  

    Oh my oh my oh my the amount of times i've fallen stray to that little error Big Smile

     

    Basically I'll bet money yuo added the try later.

     

    Your variable myClass has fallen out of scope when the try block finishes.

     

    so change to this.

     

    FileReaderClass myClass = null;

    try

    {

    myClass = new FileReaderClass();

    }

    catch(Exception Ex)

    {

    Console.WriteLine(Ex.ToString());

    }

     

    ...

     

    foreach (string file in fileArray)

    {

    myClass.totals(file, targetDir);

    }

     

     

    And curse youself for you shall make that mistake 100 times before it sinks in Big Smile

    Tuesday, July 31, 2007 11:33 AM
  • Hi Im afraid that didnt work...

     

    Here is my ammended code... and it still doesnt work... any ideas why it still gives me the error

     

    Error 1 The name 'myClass' does not exist in the current context C:\DemoProjects\dotnet2filethingy\dotnet2filethingy\Program.cs 49 17 FileReader

     

    and it gives it twice...

     

     

    using System;

    using System.IO;

    using System.Collections;

    using System.Text;

    // CODE_REVIEW - Email - Namespace should be something like Nationwide.Intranet."applicationName" (you will have to check what teh Intranet Application Namespace is.

    //NB if it was an Internet Bank application it would have Nationwide.Olb."applicationName"

    namespace FileReader

    {

    //CODE_REVIEWIdea update summary information to be something more meaningful. It should be a summary of what your class does.

    /// <summary>

    /// Summary description for Class1.

    /// </summary>

    // CODE_REVIEW - Email- class name should not be class 1.

    class FileReaderClass

    {

    const string COMMA = ",";

    const string DTCLIENT = "<DTCLIENT>";

    const string USERID = "<USERID>";

    const string APPVER = "<APPVER>";

    const string RIGHTCHEVRON = ">";

    const string EMPTYSTRING = "";

    const string OPENING_REQUEST = "Please Enter the target directory";

    const string ERROR_1 = "Target Directory Does Not Exist,Please Enter a valid target directory";

     

    /// <summary>

    /// The main entry point for the application.

    /// </summary>

    [STAThread]

    static void Main(string[] args)

    {

     

    FileReaderClass myclass = null;

    try

    {

    myClass = new FileReaderClass();

    }

    catch(Exception Ex)

    {

    Console.WriteLine(Ex.ToString());

    }

    //CODE_REVIEWEmail -You need to put some error handling round this - try catch finally.

    //CODE_REVIEWIdea - string literals ie "Please Enter the target directory" should either be constants or be in a resource file

    // To set a constant up you write it within the class as private const string EnterTargetDirectory = "Please Enter the target directory";

    Console.WriteLine(OPENING_REQUEST);

    string targetDir = Console.ReadLine();//set the target directory

    bool directoryExists = Directory.Exists(targetDir);

    while (Directory.Exists(targetDir) == false)

    {

    //CODE_REVIEWIdea - string Literals should either be constants or in a resource file

    Console.WriteLine(ERROR_1);

    targetDir = Console.ReadLine();

    }

    string[] fileArray = Directory.GetFiles(targetDir);

    foreach (string file in fileArray)

    {

    myClass.totals(file, targetDir);

    }

    }

    //CODE_REVIEWEmail - Need to put some summary informtion in. Just do /// above the private void and it will do some of it for you

    private void totals(string fileName,string targetDir)

    {

    int needTitle = 0;

    int needNewLine = 0;

    //CODE_REVIEWIdea - string literals need to be constants

    StreamWriter sw = new StreamWriter(targetDir + "/extractedData.csv", true);

    //CODE_REVIEWIdea - All commented and unused code should be removed from the solution

    //CODE_REVIEWIdea - always initialise variables unless yiou are assigning a value straight away.

    //If you need your count range to include 0 then set it to -1 or something similar

    int count;

     

    //CODE_REVIEWIdea - give rw a more meaningful name otherwise it will not pass the fxcop

    TextReader rw = new StreamReader(fileName);

    ////CODE_REVIEWIdea - initialise string ie string compareString = string.Empty;

    string compareString;

    while((compareString = rw.ReadLine()) != null)//assign each line into the comapre string white space is still a string

    {

    //CODE_REVIEWIdea - -1 needs to be a meaningful constant

    //CODE_REVIEWIdea - string literals need to be constants

    if (compareString.IndexOf(DTCLIENT) != -1)//see if the line contains the tag

    {

    needNewLine++;

     

    //CODE_REVIEWIdea - string literals need to be constants

    count = compareString.IndexOf(RIGHTCHEVRON) + 1;

    //write to csv database

    compareString = compareString.Remove(0, count);

    sw.Write(compareString);

    //CODE_REVIEWIdea - string literals need to be constants

    sw.Write(COMMA);

     

    Console.WriteLine(compareString);

    }

     

    //CODE_REVIEWIdea - string literals need to be constants

    else if (compareString.IndexOf(USERID) != -1)//see if the line contains the tag

    {

    needNewLine++;

    //CODE_REVIEWIdea - string literals need to be constants

    count = compareString.IndexOf(RIGHTCHEVRON) + 1;

    //write to csv database...

    compareString = compareString.Remove(0,count);

    sw.Write(compareString);

    //CODE_REVIEWIdea - string literals need to be constants

    sw.Write(COMMA);

    Console.WriteLine(compareString);

    }

    //CODE_REVIEWIdea - string literals need to be constants

    else if (compareString.IndexOf(APPVER) != -1)//see if the line contains the tag

    {

    needNewLine++;

    //CODE_REVIEWIdea - string literals need to be constants

    count = compareString.IndexOf(RIGHTCHEVRON) + 1;

    //write to csv database

    compareString = compareString.Remove(0, count);

    sw.Write(compareString);

    //CODE_REVIEWIdea - string literals need to be constants

    sw.Write(COMMA);

    Console.WriteLine(compareString);

    }

    //CODE_REVIEWIdea -make 3 a const with a name which is meaningful to the process.

    if(needNewLine >= 3)

    {

    needNewLine = 0;

    //CODE_REVIEWIdea -use string.Empty rather than ""

    sw.WriteLine(EMPTYSTRING);

    }

    }

    //CODE_REVIEWIdea - Remove commented out code

    // sw.WriteLine("/n");

    rw.Close();

    sw.Close();

     

    }

    //CODE_REVIEWEmail - Redundant code should be removed.

    //CODE_REVIEWEmail - If this is going to be used put summary information in

    private void writeValuesToExcel(int columnRef, string value)//columnRef 1: DTClient

    {

    //open excel spreadsheet

    //write data to it

    //close excel spreadsheet

    }

    }

    }

     

    Tuesday, July 31, 2007 12:07 PM
  •  

    Classic mistake number TWO Smile

     

    Cut and paste

     

    Anyway mate you have written

     

    FileReaderClass myclass = null;

    try

    {

    myClass = new FileReaderClass();

    }

     

     

    Of course C# is case sensitive so you need

     

    FileReaderClass myClass = null;

    try

    {

    myClass = new FileReaderClass();

    }

     

     

     

    note myclass ---> myClass

    Tuesday, July 31, 2007 12:58 PM