none
Stack Overflow exception. RRS feed

  • Question

  • Hi,

    I am getting a Stack Overflow exception during runtime. After googling it, some suggestions are that I look for any loops that have no exit. I have tried to review my code, and I can't seem to find where the issue is. Maybe a fresh set of eyes can help me.

    Below is the code.

    Code Snippet

    class Consolidate
        {
            //create properties
            private static int BatchCount
            {
                get
                {
                    return BatchCount;
                }
                set
                {
                    BatchCount = value;
                }
            }
            private static int EntryDetCount
            {
                get
                {
                    return EntryDetCount;
                }
                set
                {
                    EntryDetCount = value;
                }
            }
            private static int TotalDeb
            {
                get
                {
                    return TotalDeb;
                }
                set
                {
                    TotalDeb = value;
                }
            }
            private static int TotalCred
            {
                get
                {
                    return TotalCred;
                }
                set
                {
                    TotalCred = value;
                }
            }
            private static int EntryDetailCount
            {
                get
                {
                    return EntryDetailCount;
                }
                set
                {
                    EntryDetailCount = value;
                }
            }
            private static int EntryHash
            {
                get
                {
                    return EntryHash;
                }
                set
                {
                    EntryHash = value;
                }
            }

            //set flags
            private static bool isFileHeaderWritten = false;

            private static int BatchNum = 1;
            //this class will conolidate the files.
            public static void ConsFiles(string[] FileToCons, bool isTest)
            {
                //consolidate the file
                /*************************************************************************************************************
                 * Lines
                 * File Header              Starts with     "1"         # of Fields     13
                 * Company/Batch Header     Starts with     "5"         # of Fields     13
                 * Entry Detail             Starts with     "6"         # of Fields     11
                 * Company/Batch Control    Starts with     "8"         # of Fields     11
                 * File Control             Starts with     "9"         # of Fields     8
                 * Filling                  Starts with     "9"         # of Fields     N/A - streatches full width 94 chars
                 *
                 *          MUST MAKE LINES MOD 10 = 0
                 * *************************************************************************************************************/

                /***************************************************************************************************************
                 * Calculations by line
                 * File Header - unchanged
                 * Compnay/Batch Header - must update field 13, pos 88 - 94, Batch Number. Sequential for each batch in file
                 * Entry Detail - unchanged
                 * Company/Batch Control - must update field 11, pos 88-94, Batch Number to equal field 13 of comp/batch header
                 * File Control - update Field 2, pos 2-7, Batch Count
                 *                       Field 3, pos 8-13, Block Count
                 *                       Field 4, pos 14-21, Entry Det Count
                 *                       Field 6, pos 32-43, Total Debit
                 *                       Field 7, pos 44-55, Total Credit
                 * *************************************************************************************************************/
                
                //set record ID string and Fields
                string RecID;

                FileServer fs = new FileServer();
               
                //iterate through file
                foreach (string Record in FileToCons)
                {
                    //substring first char of record, will determine record type
                    RecID = Record.Substring(0, 1);
                    if (RecID == "1")
                    {
                        if (isFileHeaderWritten != true)
                        {
                            //this is the File Header record
                            fs.WriteFile(Record, FileServer.FileWriteType.CONS, isTest);
                            //set flag. We will test for this flag latter on
                            isFileHeaderWritten = true;
                        }
                    }
                    else if (RecID == "5")
                    {
                        //this is the company/batch header record
                        //Need to handle BatchNum, BatchCount
                        if (isFileHeaderWritten)
                        {
                            //Create field vars.
                            //we will have to recreate this line of the file with every iteration
                            string RecTypeField = Record.Substring(0, 1);
                            string ServiceClassField = Record.Substring(2, 3);
                            string CompNameField = Record.Substring(5, 16);
                            string CompDiscretField = Record.Substring(21, 20);
                            string CompIDField = Record.Substring(41, 10);
                            string StandEntryClassField = Record.Substring(51, 3);
                            string CompEntryDescField = Record.Substring(54, 10);
                            string CompDescriptiveDateField = Record.Substring(64, 6);
                            string EffEntryDateField = Record.Substring(70, 6);
                            string SettlementDateField = Record.Substring(76, 3);
                            string OriginatorStatusField = Record.Substring(79, 1);
                            string OrigDFIIDField = Record.Substring(80, 8);
                            string BatchNumberField = null;

                            //iterate through, backfilling with zeroes the BatchNumberField from the BatchNum var.
                            for (int i = BatchNum.ToString().Length; i < 8; i++)
                            {
                                BatchNumberField = "0" + BatchNum.ToString();
                            }

                            //write the new line of the file
                            string line = RecTypeField + ServiceClassField + CompNameField + CompDiscretField + CompIDField + StandEntryClassField + CompEntryDescField + CompDescriptiveDateField + EffEntryDateField + SettlementDateField + OriginatorStatusField + OrigDFIIDField + BatchNumberField;
                            fs.WriteFile(line, FileServer.FileWriteType.CONS, isTest);
                           
                            //increment BatchNum and BatchCount
                            BatchNum++;
                            BatchCount++;
                        }
                    }
                    else if (RecID == "6")
                    {
                        //this is the entry detail record
                        //will need to handle EntryDetCount
                        if (isFileHeaderWritten)
                        {
                            //write the record, increment the EntryDetCount var for later use
                            fs.WriteFile(Record, FileServer.FileWriteType.CONS, isTest);
                            EntryDetCount++;
                        }
                    }
                    else if (RecID == "8")
                    {
                        //this is the company/batch control record
                        if (isFileHeaderWritten)
                        {
                            //convert Batch Debit to int for calculation
                            int BatchDebField = Convert.ToInt32(Record.Substring(21, 12));
                            //convert Batch Credit to int for calculation
                            int BatchCredField = Convert.ToInt32(Record.Substring(33, 12));
                            //write the record
                            fs.WriteFile(Record, FileServer.FileWriteType.CONS, isTest);
                            //perform calculations
                            TotalDeb = TotalDeb + BatchDebField;
                            TotalCred = TotalCred + BatchCredField;
                        }
                    }
                    else if (RecID == "9")
                    {
                        string SubRecID = Record.Substring(1, 1);
                        if (SubRecID != "9")
                        {
                            //This is not the filling, which we are not concerned with.
                            //this is the File Control record
                            EntryDetCount = EntryDetCount + Convert.ToInt32(Record.Substring(14, 8));
                            EntryHash = EntryHash + Convert.ToInt32(Record.Substring(22, 10));
                        }
                    }
               }
            }


    The method, ConsFiles, takes a string array and tests each line in the string array for certain shared record types, and performs actions needed for each line, eventually consolidating a number of files into one file. For example, if the record type is "1" then I only want to write that record to the consolidated file once. Others require different actions to take place. When I step through the code, using a breakpoint, I can normally make it to the end of the first iteration of the foreach loop, before I get the error. If I don't step through it, and just let it run without breakpoints, it will produce the error at the get block of the BatchCount property of the class. I have tried various solutions (calling both static and inherited methods for the WriteFile() method as well as the calling method to this class. Any ideas would be helpful, I can't seem to find where the logic error is.

    Thanks!

    Wednesday, April 23, 2008 5:17 PM

Answers

  • Your property setters/getters are coded wrong.  Code them like this:

        private static int mBatchCount;
        private static int BatchCount {
          get {
            return mBatchCount;
          }
          set {
            mBatchCount = value;
          }
        }

    BTW, private properties like this aren't very useful.  You might as well use the field.
    Wednesday, April 23, 2008 5:34 PM
    Moderator

All replies

  • Your property setters/getters are coded wrong.  Code them like this:

        private static int mBatchCount;
        private static int BatchCount {
          get {
            return mBatchCount;
          }
          set {
            mBatchCount = value;
          }
        }

    BTW, private properties like this aren't very useful.  You might as well use the field.
    Wednesday, April 23, 2008 5:34 PM
    Moderator
  • That did it!

    Thanks for the answer, and sorry about the elementary nature of it.

    Also, thanks for setting me straight on best practices!!!!

    Thanks again!
    Wednesday, April 23, 2008 9:59 PM