Link to home
Start Free TrialLog in
Avatar of zimmer9
zimmer9Flag for United States of America

asked on

How would you abort a C# Windows platform application if an array were to exceeds its boundaries "Index was outside the bounds of the array"?

I am developing a C# application using VS2005 on a windows platform. How would you modify the following snippet of code to end the program immediately if arr1 > 99000
and write the error "Index was outside the bounds of the array" to the file Check.err.txt

string[,] arr1 = new string[99000, 20];

-----------------------------------------------------

using System;
using System.Xml;
using System.IO;
using System.Text.RegularExpressions;
using System.Globalization;
using System.Collections.Generic;


namespace ReadXml1
{

    class Class1
    {
        static void Main(string[] args)
        {

            string goodFilePath = @"\\v\r7.1.ard.ind";
            string errorFilePath = @"\\v\Check.err.txt";

           
            if (File.Exists(goodFilePath))
            {
                File.Delete(goodFilePath);
            }
            if (File.Exists(errorFilePath))
            {
                File.Delete(errorFilePath);
            }

            DirectoryInfo parentDirectory = new DirectoryInfo(@"\\v\r");

           
            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 (file != null && XmlFile(file.FullName))

                    {
                        StreamWriter sw1 = new StreamWriter(errorFilePath);
                        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\prod\HarrisCheck\data\HRSBK.HARRISBK.CHKHARB4.CHKHARB4.77.1.ard.out");                        
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                finally
                {
                    if (sw != null) sw.Close();
                }
            }


            if (File.Exists(errorFilePath))
            {
                if (new FileInfo(errorFilePath).Length == 0)
                {
                    if (File.Exists(errorFilePath))
                    {
                        File.Delete(errorFilePath);
                    }
                }

                else
                {
                    if (File.Exists(goodFilePath))

                        File.Delete(goodFilePath);                    
                }
            }


            Console.WriteLine("Processing done. Press enter to exit ... ");
            Console.ReadLine();
        }

private static void ProcessFile(string filePath, StreamWriter sw, StreamWriter sw1)
        {
            int i = 0;
            string[,] arr1 = new string[99000, 20];       //where array can exceed 99,000 records
            int iCheckNumber = 0;
            String formattedString1 = null;
            String itemsequence = null;
            XmlTextReader reader = null;
            try
            {
                reader = new XmlTextReader(filePath);
                while (reader.Read())
                {

                    if (reader.NodeType == XmlNodeType.Element)
                    {

                        if (reader.Name == "csc:item_sequence_number")
                        {
                            arr1[i, 11] = "GROUP_FIELD_NAME:CpcsNo";
                            itemsequence = reader.ReadElementContentAsString();
                            String formattedString = itemsequence.Substring(7, 8);
                            Int64 intTest = 0;

                            if (Int64.TryParse(formattedString, out intTest) && intTest > 0)
                                arr1[i, 12] = "GROUP_FIELD_VALUE:" + formattedString;
                            else
                            {
                                arr1[i, 12] = "GROUP_FIELD_VALUE:" + formattedString;
                                sw1.WriteLine("GROUP_FIELD_NAME:ItemSequence:" + itemsequence + ",GROUP_FIELD_NAME:CpcsNo" + "," + "GROUP_FIELD_VALUE:" + formattedString);
                                iCheckNumber--;
                            }
                        }
                        if (reader.Name == "csc:check_number")
                        {
                            iCheckNumber = iCheckNumber + 1;
                            arr1[i, 0] = "COMMENT: CHECK NUMBER #" + iCheckNumber;
                            arr1[i, 1] = "GROUP_FIELD_NAME:CheckNumber";
                            String e = reader.ReadElementContentAsString();
                            String formattedString = e.Substring(6, 3);
                            formattedString1 = e.Substring(9, 6);
                            Int64 intTest = 0;

                            if (Int64.TryParse(formattedString1, out intTest))
                                arr1[i, 2] = "GROUP_FIELD_VALUE:" + formattedString1;
                            else
                            {
                                arr1[i, 2] = "GROUP_FIELD_VALUE:" + formattedString1;
                                sw1.WriteLine("GROUP_FIELD_NAME:ItemSequence:" + itemsequence + ",GROUP_FIELD_NAME:CheckNumber" + "," + "GROUP_FIELD_VALUE:" + formattedString1);
                            }

                            arr1[i, 15] = "GROUP_FIELD_NAME:OfficeNo";
                            if (Int64.TryParse(formattedString, out intTest) && intTest > 0)
                                arr1[i, 16] = "GROUP_FIELD_VALUE:" + formattedString;
                            else
                            {
                                arr1[i, 16] = "GROUP_FIELD_VALUE:" + formattedString;
                                sw1.WriteLine("GROUP_FIELD_NAME:ItemSequence:" + itemsequence + ",GROUP_FIELD_NAME:OfficeNo" + "," + "GROUP_FIELD_VALUE:" + formattedString);
                            }
                        }
                       

                sw.WriteLine("COMMENT: specify code page of the index date");
                sw.WriteLine("CODEPAGE:819");
                for (int iter = 0; iter < i; iter++)
                {
                    for (int j = 0; j < 20; j++)
                    {
                        sw.WriteLine(arr1[iter, j]);
                    }
                }
            }
            finally
            {
                if (sw1 != null) sw1.Close();
                reader.Close();
            }
        }

 static bool XmlFile(string filePath)
        {
            string line;
            bool xmlFile = false;
            StreamReader sr = null;
            try
            {
                sr = new StreamReader(filePath);
                line = sr.ReadLine();
                if (line.Equals("<?xml version=\"1.0\"?>"))
                    xmlFile = true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                xmlFile = false;
            }
            finally
            {
                if (sr != null) sr.Close();
            }
            return xmlFile;
        }

    }
}
SOLUTION
Avatar of jmcmunn
jmcmunn
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial