-1

I keep getting the error

An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll

when inputting a value over 70 characters in my code, could anybody please explain why?

namespace testingStrategiesCoding
{
    class Program
    {
        static void Main(string[] args)
        {
            string userMessage;
            int messageLength;
            string newMessage1;
            string newMessage2;
            string newMessage3;

            Console.WriteLine("Enter a message");
            userMessage = Console.ReadLine();

            messageLength = userMessage.Length;

            if (messageLength < 71)
            {
                Console.WriteLine("");
                Console.WriteLine(userMessage);
            }

            else if (messageLength > 70 && messageLength < 141)
            {
                newMessage1 = userMessage.Substring(0, 70);
                newMessage2 = userMessage.Substring(71, messageLength);
                Console.WriteLine("");
                Console.WriteLine(newMessage1);
                Console.WriteLine("");
                Console.WriteLine(newMessage2);
            }

            else if (messageLength > 140 && messageLength < 211)
            {
                newMessage1 = userMessage.Substring(0, 70);
                newMessage2 = userMessage.Substring(71, 140);
                newMessage3 = userMessage.Substring(141, messageLength);
                Console.WriteLine("");
                Console.WriteLine(newMessage1);
                Console.WriteLine("");
                Console.WriteLine(newMessage2);
                Console.WriteLine("");
                Console.WriteLine(newMessage3);
            }   
            else
            {
                Console.WriteLine("Invalid, please enter a message lower than 210 characters.");
            }

            Console.ReadKey();
        }
    }
}

I do not think this is a duplicate due to the error being a factor of a part of the code that no other thread seems to relate to.

  • `newMessage2 = userMessage.Substring(71, messageLength);` => `newMessage2 = userMessage.Substring(71);` – Martin Verjans Mar 05 '18 at 10:09
  • Possible duplicate of [What is an IndexOutOfRangeException / ArgumentOutOfRangeException and how do I fix it?](https://stackoverflow.com/questions/20940979/what-is-an-indexoutofrangeexception-argumentoutofrangeexception-and-how-do-i-f) – Camilo Terevinto Mar 05 '18 at 10:12

2 Answers2

10
Substring(71, messageLength)

The second substring parameter is the length, not the end-index. You need to subtract the start-index for this to work. Also, you might want to start with 70, or you lose a character.

Substring(70, messageLength - 70)
Manfred Radlwimmer
  • 13,257
  • 13
  • 53
  • 62
0

To avoid the issue altogether, consider using MoreLINQ's Batch (to split the string into 70 character 'chunks' / 'batches').

The advantage of this is that you avoid having to worry about getting offsets wrong etc.

The code would look something like:

using System;
using MoreLinq;

namespace MattConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter a message");
            var userMessage = Console.ReadLine();

            if (userMessage?.Length <= 210)
            {
                var batchedData = userMessage.Batch(70);

                foreach (var entry in batchedData)
                {
                    Console.WriteLine("");
                    var asString = string.Concat(entry);
                    Console.WriteLine(asString);
                }
            }
            else
            {
                Console.WriteLine("Invalid, please enter a message lower than 210 characters.");
            }

            Console.ReadKey();
        }
    }
}
mjwills
  • 23,389
  • 6
  • 40
  • 63