Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 136,157 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 2,128 people online right now. Registration is fast and FREE... Join Now!




Logic Issue

 
Reply to this topicStart new topic

Logic Issue, I'm lazy today, wanted to see if i could get suggestions

tody4me
17 Dec, 2007 - 01:43 PM
Post #1

Only Jenny Craig makes thin clients...
Group Icon

Joined: 12 Apr, 2006
Posts: 1,278



Thanked: 3 times
Dream Kudos: 100
My Contributions
Well, it's the week before christmas, and all thru the brain, there's nothing but cobwebs, hanging from the top of the skull to the bottom. Not that this isn't always the case, but at least this week I have an excuse smile.gif. I wanted to know, if someone would be so kind as to help me with a small portion of logic that i'm working on. What this is designed to do is take a string, regardless of length, and split it in chunks of 60 characters, but not to truncate words, or word parts. Here's the code, or at least an excerpt.

CODE

            if (headerRow[4].ToString().Length > 60)
            {
                lenHeadString = 60;
                for (int curHead = nextWritten; curHead < rowCt; curHead++)
                {
                    curHeadString = headerRow[4].ToString().Substring(nextHeadPos, lenHeadString).TrimStart();
                    if (curHeadString.LastIndexOfAny(new char[] {' ', '-', '$', '/' }) != -1 && !bEnd)
                        curHeadString = curHeadString.Substring(0, curHeadString.LastIndexOfAny(new char[] {' ', '-', '$', '/'}));
                    else
                    {
                        if (bEnd)
                            curHeadString = headerRow[4].ToString().Substring(nextHeadPos, lenHeadString);
                    }
                    nextHeadPos += curHeadString.Length + 1;
                    if ((headerRow[4].ToString().Length - nextHeadPos) >= 60)
                        lenHeadString = 60;
                    else
                    {
                        lenHeadString = (headerRow[4].ToString().Length - nextHeadPos);
                        bEnd = true;
                    }
                    docMiddle[nextWritten++, 2] = curHeadString;
                }
            }
            else
                docMiddle[nextWritten++, 2] = headerRow[4];
            return docMiddle;


To say the least, it's very cracked. The nextWritten variable is defined to be global, and is to keep track of the location in the document the next written line is. The rowCt var is defined in a calling function, and is passed into this function. The docMiddle is a 2 dimensional array that is defined in a calling function, and is passed by ref to this function. Basically, where it's failing is if the string is > 15 characters without a break, such as tracking numbers, or other run-togethers that are passed into this. In the failed test it truncated to 3 lines, as it should, but the last line was not written (4th in this case, should not have happened but it did due to the above situation), therefore causing the data that was going to be on the 4th line to be truncated. Basically the logic so far is to take the length of the string, divide it by the size of a line (60) and add one to get the row count. Then from there, parse on a max length of the specified line size, and split based on the separated characters (in this case the ' ', '-', '$', '/' character set). Like I said this works, except for in the case that it failed, string length > 15 between separators. I know this logic looks and probably is not sound, and that's another reason for posting here for suggestions.

TIA

User is offlineProfile CardPM
+Quote Post

tody4me
RE: Logic Issue
18 Dec, 2007 - 08:00 AM
Post #2

Only Jenny Craig makes thin clients...
Group Icon

Joined: 12 Apr, 2006
Posts: 1,278



Thanked: 3 times
Dream Kudos: 100
My Contributions
Ok, I found a way of fixing this. It's not very pretty, but it works... for now.
added the following
CODE

if (lenHeadString <= 0)
   break;


and put it right after the statement
CODE

for (int curHead = nextWritten; curHead < rowCt; curHead++)


This works, because I incremented the rowCt by one, therefore allowing for the anomaly.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/1/08 11:21PM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month