?
Solved

ArgumentOutOfRangeException

Posted on 2014-10-18
2
Medium Priority
?
92 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month12 days, 11 hours left to enroll

777 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