Solved

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

Posted on 2011-09-02
4
251 Views
Last Modified: 2013-12-17
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
Comment
Question by:zimmer9
  • 2
4 Comments
 
LVL 10

Expert Comment

by:Irzana
ID: 36476616
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
 

Author Comment

by:zimmer9
ID: 36476749
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
 
LVL 7

Accepted Solution

by:
Gewgala earned 500 total points
ID: 36476752
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
 
LVL 7

Expert Comment

by:Gewgala
ID: 36476757
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SQL Help 27 45
Expression Evaluater 3 24
Calculate number of nights between two dates 5 21
SQL Query Help Top 1 and Distinct? 6 25
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question