• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 272
  • Last Modified:

How to write an ELSE clause routine so that it only gets executed 1 time in a C# program?

I am writing a C# console application using Visual Studio 2005.

Is there a way to rewrite the following code in the code section below
so that the ELSE clause only gets executed 1 time?


foreach (FileInfo file in parentDirectory.GetFiles())
{
   StreamWriter sw = null;
   try
   {
      if (XmlFile(file.FullName))
      {
          sw = new StreamWriter(goodFilePath);
          ProcessFile(file.FullName, sw, sw1);
      }
      else
      {
          sw = new StreamWriter(emptyFilePath);
          File.Copy(file.FullName, @"\\v\region\na\appl\ctrls\cashcontrol\data\dev\HarrisCheck\data\HRSBK.HARRISBK.THKHARB4.THKHARB4.77.1.ard.out");
      }
   }
   catch (Exception ex)
   {
       Console.WriteLine(ex.Message);
   }
   finally
   {
       if (sw != null) sw.Close();
   }
}

Open in new window

0
zimmer9
Asked:
zimmer9
  • 2
1 Solution
 
IrzanaCommented:
can you tell the purpose?
That mean do you want to come out of for loop once if goes to else? if so you can add a break with in else
0
 
zimmer9Author Commented:
I want to continue looping through for each file
but when I hit the ELSE clause, I only want it to perform the statements in the ELSE clause 1 time.
0
 
GewgalaCommented:
You can add a break statement as was previously mentioned.  This will mean that once the else block is hit, the loop will exit entirely on the iteration it was currently on without finishing to the end of the collection/array (see in-line commentary below):

foreach (FileInfo file in parentDirectory.GetFiles())
{
   StreamWriter sw = null;
   try
   {
      if (XmlFile(file.FullName))
      {
          sw = new StreamWriter(goodFilePath);
          ProcessFile(file.FullName, sw, sw1);
      }
      else
      {
          sw = new StreamWriter(emptyFilePath);
          File.Copy(file.FullName, @"\\v\region\na\appl\ctrls\cashcontrol\data\dev\HarrisCheck\data\HRSBK.HARRISBK.THKHARB4.THKHARB4.77.1.ard.out");
          break;  // <-- this will cause the loop to exit and immediately proceed to the finally block
      }
   }
   catch (Exception ex)
   {
       Console.WriteLine(ex.Message);
   }
   finally
   {
       if (sw != null) sw.Close();
   }
}

Open in new window


Or you can do something like this.  The below will continue iterating through the loop, but the change from else to else if will make it so the else if block is only hit 1 time, but will still allow the loop to finish to the end (see in-line comments below):

bool hasElseBeenExecuted = false;  // <-- add this boolean line to use as a check further down

foreach (FileInfo file in parentDirectory.GetFiles())
{
   StreamWriter sw = null;
   try
   {
      if (XmlFile(file.FullName))
      {
          sw = new StreamWriter(goodFilePath);
          ProcessFile(file.FullName, sw, sw1);
      }
      else if (!hasElseBeenExecuted)  // <-- change this from a regular "else" to an "else if" that specifically looks to see if the hasElseBeenExecuted variable is set to false
      {
          hasElseBeenExecuted = true;  // <-- set the boolean flag to true so that this else block never gets hit again
          sw = new StreamWriter(emptyFilePath);
          File.Copy(file.FullName, @"\\v\region\na\appl\ctrls\cashcontrol\data\dev\HarrisCheck\data\HRSBK.HARRISBK.THKHARB4.THKHARB4.77.1.ard.out");
      }
   }
   catch (Exception ex)
   {
       Console.WriteLine(ex.Message);
   }
   finally
   {
       if (sw != null) sw.Close();
   }
}

Open in new window

0
 
GewgalaCommented:
I posted the above comment before I saw you were wanting to finish the loop execution.  The second part of my above comment should be what you need if I understand you correctly.  It will allow loop execution to proceed but will only allow the else block to be hit one time.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now