Solved

How would you modify a function to write out 2 statements only for the 1st occurrence of a program loop in C# using VS2010?

Posted on 2014-01-07
1
446 Views
Last Modified: 2014-01-07
I am writing a C# console application in VS2010.

How would you modify the following function so that the 2 lines marked with asterisks are
only executed for the 1st program loop?

This way, these 2 WriteLine statements are only written out the 1st time throught the loop.
==========================================================================

private static void ProcessFile(string filePath, StreamWriter sw1)
        {
            for (int i = 0; i < bytes.Length; i++)
            {                
                if (data.SequenceEqual(anchor1))
                {                  
                    for (int k = i + anchor1.Length; k < bytes.Length; k++)
                    {

                        if (data2.SequenceEqual(anchor2))
                        {
                            string indFilePath = System.IO.Path.Combine(MyGlobals.BASE_DIR, MyGlobals.BASE_FILE_Name + ".ard.ind");
                            using (StreamWriter sw = new StreamWriter(indFilePath, true))
                            {
Execute once>**      sw.WriteLine("COMMENT: specify code page of the index date");
Execute once>**      sw.WriteLine("CODEPAGE:819");
                                sw.WriteLine(commentchecknum);
                                sw.WriteLine(groupNameCheckNumber);                                
                                sw.Close();
                            }
                            MyGlobals.intcheckOffset = MyGlobals.intcheckOffset + intcheckimagelength;
                            break;
                        }
                    }
                    sw1.Close();
                }
            }
        }
0
Comment
Question by:zimmer9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 39764167
Hi zimmer9;

This modification to the code should do what you need.

private static void ProcessFile(string filePath, StreamWriter sw1)
{
    bool firstTimeFlag = true;
    for (int i = 0; i < bytes.Length; i++)
    {                
        if (data.SequenceEqual(anchor1))
        {                   
            for (int k = i + anchor1.Length; k < bytes.Length; k++)
            {

                if (data2.SequenceEqual(anchor2))
                {
                    string indFilePath = System.IO.Path.Combine(MyGlobals.BASE_DIR, MyGlobals.BASE_FILE_Name + ".ard.ind");
                    using (StreamWriter sw = new StreamWriter(indFilePath, true))
                    {
                        if( firstTimeFlag )
                        {
                            sw.WriteLine("COMMENT: specify code page of the index date");
                            sw.WriteLine("CODEPAGE:819");
                            firstTimeFlag = false;
                        }
                        sw.WriteLine(commentchecknum);
                        sw.WriteLine(groupNameCheckNumber);                                
                        sw.Close();
                    }
                    MyGlobals.intcheckOffset = MyGlobals.intcheckOffset + intcheckimagelength;
                    break;
                }
            }
            sw1.Close();
        }
    }
}

Open in new window

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
CSS question 16 59
c#  FTP ftpwebrequest URI invalid 6 41
What!!!??? 5 58
Visual Studio Start / Browser Emulator menu different 2 33
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

738 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