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
434 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
1 Comment
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
Comment Utility
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

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
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…

728 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

9 Experts available now in Live!

Get 1:1 Help Now