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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

912 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

27 Experts available now in Live!

Get 1:1 Help Now