Solved

ArgumentOutOfRangeException

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

729 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