Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
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
Medium Priority
?
456 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 64

Accepted Solution

by:
Fernando Soto earned 2000 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

636 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