Solved

ArgumentOutOfRangeException

Posted on 2014-10-18
2
84 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

813 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