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

Get Actionable Data from Your Monitoring Solution

Your communication platform is only as good as the relevance of the information you send. Ensure your alerts get to the right people every time with actionable responses. Create escalation rules that ensure everyone follows the process and nothing is left to chance.

Question has a verified solution.

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

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…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

717 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