locked
2 Compile errors I can't figure out... RRS feed

  • Question

  • I am getting the 2 below compile errors and don't understand why as the syntax looks good to me...

    1) Cannot use local variable 'fileName' before it is declared         

    2) A local variable named 'fileName' cannot be declared in this scope because it would give a different meaning to 'fileName', which is already used in a 'parent or current' scope to denote something else             

     

    folderPaths = new string[2]
    {
      Location + wbName,
      Location1 + wbName
    }
    fileNames = new string[3]
    {
      "Day1",
      "Day2",
      "Day3",
    }
    foreach (string fileName in fileNames)
    {
      foreach (string folderPath in folderPaths)
      {
        var path = Path.Combine(folderPath + fileName);
        var xlsPath = path + ".xls";
        var xlsxPath = path + ".xlsx";
        var fileName = File.Exists(xlsPath) ? xlsPath : File.Exists(xlsxPath) ? xlsxPath : null;
          if (fileName != null)
          {
             //Do Stuff here
          }
      }
    }
    
    

    Thursday, March 13, 2014 1:03 PM

Answers

  • Try this

    folderPaths = new string[2]
    {
      Location + wbName,
      Location1 + wbName
    }
    fileNames = new string[3]
    {
      "Day1",
      "Day2",
      "Day3",
    }
    foreach (string fileName in fileNames)
    {
      foreach (string folderPath in folderPaths)
      {
        var path = Path.Combine(folderPath + fileName);
        var xlsPath = path + ".xls";
        var xlsxPath = path + ".xlsx";
        var fileName1 = File.Exists(xlsPath) ? xlsPath : File.Exists(xlsxPath) ? xlsxPath : null;
          if (fileName1 != null)
          {
             //Do Stuff here
          }
      }
    }
    Error is because you have declared variable fileName twice once in for loop and then var fileName Just change any one variable name.

    Ashish Pandey


    Thursday, March 13, 2014 1:28 PM

All replies

  • Its this line:

    var fileName = File.Exists(xlsPath) ? xlsPath : File.Exists(xlsxPath) ? xlsxPath : null;

    This declares a variable called fileName, which is clashing with the fileName variable you declared in the first foreach statement.


    Thursday, March 13, 2014 1:17 PM
  • Try this

    folderPaths = new string[2]
    {
      Location + wbName,
      Location1 + wbName
    }
    fileNames = new string[3]
    {
      "Day1",
      "Day2",
      "Day3",
    }
    foreach (string fileName in fileNames)
    {
      foreach (string folderPath in folderPaths)
      {
        var path = Path.Combine(folderPath + fileName);
        var xlsPath = path + ".xls";
        var xlsxPath = path + ".xlsx";
        var fileName1 = File.Exists(xlsPath) ? xlsPath : File.Exists(xlsxPath) ? xlsxPath : null;
          if (fileName1 != null)
          {
             //Do Stuff here
          }
      }
    }
    Error is because you have declared variable fileName twice once in for loop and then var fileName Just change any one variable name.

    Ashish Pandey


    Thursday, March 13, 2014 1:28 PM
  • In your foreach loop, you use fileName, but then you declare a var in that name again.

    Just change one of them.

     

    Noam B.


    Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...

    Thursday, March 13, 2014 1:36 PM