Solved

ArgumentOutOfRangeException

Posted on 2014-10-18
2
83 Views
Last Modified: 2014-10-20
Ive been looking at a way to split a long string into segments, and come up with the following function:-
        static string[] Split(string str, int chunkSize)
        {
            int arraySize = new int();

            if (str.Length % chunkSize == 0) {
                arraySize = str.Length / chunkSize;
            } else {
                arraySize = (str.Length / chunkSize)+1;
            }

            string[] n = new string[arraySize]; 

            for (int x = 1; x<=arraySize; x++) {
                n[x] = str.Substring((x * chunkSize), (x * (chunkSize - 1)));
            }
            return n;
        }

Open in new window


Seems to run through fine, for first serveral runs, however then fails saying:-
An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll

I dont understand why its failing cause it doesnt seem to be out of range :-S

Im new to c# and could do with any assistance in pointing out my error.

Thank you
0
Comment
Question by:tonelm54
2 Comments
 
LVL 40
ID: 40389910
You are probably use to program in VB. In C#, when you declare an array, you do not specify the upper index, you are specifying the size of the array. Thus, string[10] is indexed from 0 to 9, not from 0 to 10 as it would be in VB.

If arraySize is 10, the following is then trying to access [10], that does not exist: x<=arraySize

Try with x<arraySize
0
 
LVL 27

Accepted Solution

by:
MikeToole earned 500 total points
ID: 40389911
I thing it fails because the substring requires that whole length must be within the string - and the last one would fail if the string length isn't an exact multiple of the split length.

Here's slightly changed approach which does allow for a short final piece.
it returns an IEnumerable rather than an array...
static IEnumerable<string> SplitToLength(string str, int length) 
{
	int piece = str.Length % length;
    for (int i = 0; i < str.Length - piece; i += length) 
        yield return str.Substring(i, length);
	if (piece > 0) 
	{
		int start = length * (str.Length / length);
		yield return str.Substring(start);
	}
}

Open in new window

The usage would be ...
var x = SplitToLength(MyString, length).ToArray();

Open in new window

... with the following a 'using' statement for the LInq extensions
using System.Linq;

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

910 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

19 Experts available now in Live!

Get 1:1 Help Now