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
248 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

706 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now