Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. System.ArgumentOutOfRangeException: Index was out of range

System.ArgumentOutOfRangeException: Index was out of range

Scheduled Pinned Locked Moved C#
csharpcssdatabaselinqhelp
13 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    computerpublic
    wrote on last edited by
    #1

    /*I have elimated some of the code in order to make it short and get straight to the point. I am noticing that my program will NOT process any of the non regular ascii characters (decimal 161 to 255 || hex A1 to FF) without the error below. All other regular ascii characters are process ok without errors.
    OUTPUT::::::::::BELOW

    count = 1
    count = 3
    count = 6

    Unhandled Exception: System.ArgumentOutOfRangeException: Index was out of range.
    Must be non-negative and less than the size of the collection.
    Parameter name: index
    at System.Collections.BitArray.Get(Int32 index)
    at Applica.Program.Main(String[] args) in C:\Documents and Settings\shampro\D
    esktop\Program.cs:line 55
    Press any key to continue . . .
    */

    using System;
    using System.IO;
    using System.Collections;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace Applica
    {
    class Program
    {
    static void Main(string[] args)
    {
    DirectoryInfo da = new DirectoryInfo("C:\\Folder7");
    FileInfo[] Arr = da.GetFiles();
    if (Arr.Length == 0)
    {
    throw new InvalidOperationException("No files found.");
    }
    FileInfo ap = Arr[Arr.Length - 1];
    ulong Totbyte = (ulong)ap.Length;
    string filePath = ap.FullName;
    string temPath = Path.GetTempFileName();
    byte[] data = File.ReadAllBytes(filePath);
    File.WriteAllBytes(temPath, data);
    byte[] dataa = new byte[1];
    for (ulong counter = 0; counter < Totbyte; counter++)
    {
    dataa[0] = data[counter];
    BitArray bits = new BitArray(dataa);
    for (int count = 0; count < bits.Length;)
    {
    if (count != 7)
    {
    if (bits[count] == false && bits[count + 1] == true)
    count++;
    if (bits[count] == false && bits[count + 1] == false)
    count++;
    if (bits[count] == true && bits[count + 1] == true)/////line 55 error
    count++;
    if (bits[count] == true && bits[count + 1] == false)
    count++;
    Console.WriteLine("count = {0}", count);
    }
    }
    }
    }

    Richard Andrew x64R Richard DeemingR B 3 Replies Last reply
    0
    • C computerpublic

      /*I have elimated some of the code in order to make it short and get straight to the point. I am noticing that my program will NOT process any of the non regular ascii characters (decimal 161 to 255 || hex A1 to FF) without the error below. All other regular ascii characters are process ok without errors.
      OUTPUT::::::::::BELOW

      count = 1
      count = 3
      count = 6

      Unhandled Exception: System.ArgumentOutOfRangeException: Index was out of range.
      Must be non-negative and less than the size of the collection.
      Parameter name: index
      at System.Collections.BitArray.Get(Int32 index)
      at Applica.Program.Main(String[] args) in C:\Documents and Settings\shampro\D
      esktop\Program.cs:line 55
      Press any key to continue . . .
      */

      using System;
      using System.IO;
      using System.Collections;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;

      namespace Applica
      {
      class Program
      {
      static void Main(string[] args)
      {
      DirectoryInfo da = new DirectoryInfo("C:\\Folder7");
      FileInfo[] Arr = da.GetFiles();
      if (Arr.Length == 0)
      {
      throw new InvalidOperationException("No files found.");
      }
      FileInfo ap = Arr[Arr.Length - 1];
      ulong Totbyte = (ulong)ap.Length;
      string filePath = ap.FullName;
      string temPath = Path.GetTempFileName();
      byte[] data = File.ReadAllBytes(filePath);
      File.WriteAllBytes(temPath, data);
      byte[] dataa = new byte[1];
      for (ulong counter = 0; counter < Totbyte; counter++)
      {
      dataa[0] = data[counter];
      BitArray bits = new BitArray(dataa);
      for (int count = 0; count < bits.Length;)
      {
      if (count != 7)
      {
      if (bits[count] == false && bits[count + 1] == true)
      count++;
      if (bits[count] == false && bits[count + 1] == false)
      count++;
      if (bits[count] == true && bits[count + 1] == true)/////line 55 error
      count++;
      if (bits[count] == true && bits[count + 1] == false)
      count++;
      Console.WriteLine("count = {0}", count);
      }
      }
      }
      }

      Richard Andrew x64R Offline
      Richard Andrew x64R Offline
      Richard Andrew x64
      wrote on last edited by
      #2

      When your loop is in its last iteration, count == bits.Length - 1, so guess what count + 1 is equal to?

      The difficult we do right away... ...the impossible takes slightly longer.

      C 1 Reply Last reply
      0
      • Richard Andrew x64R Richard Andrew x64

        When your loop is in its last iteration, count == bits.Length - 1, so guess what count + 1 is equal to?

        The difficult we do right away... ...the impossible takes slightly longer.

        C Offline
        C Offline
        computerpublic
        wrote on last edited by
        #3

        No its not. Look at the output. I ran the entire alphabet with no problem, but soon as I try to run just one or any special ascii characters like µ it gives an error.

        Richard Andrew x64R 1 Reply Last reply
        0
        • C computerpublic

          No its not. Look at the output. I ran the entire alphabet with no problem, but soon as I try to run just one or any special ascii characters like µ it gives an error.

          Richard Andrew x64R Offline
          Richard Andrew x64R Offline
          Richard Andrew x64
          wrote on last edited by
          #4

          OK, I might be wrong. I didn't trace the code, I only gave a quick look. My apologies.

          The difficult we do right away... ...the impossible takes slightly longer.

          1 Reply Last reply
          0
          • C computerpublic

            /*I have elimated some of the code in order to make it short and get straight to the point. I am noticing that my program will NOT process any of the non regular ascii characters (decimal 161 to 255 || hex A1 to FF) without the error below. All other regular ascii characters are process ok without errors.
            OUTPUT::::::::::BELOW

            count = 1
            count = 3
            count = 6

            Unhandled Exception: System.ArgumentOutOfRangeException: Index was out of range.
            Must be non-negative and less than the size of the collection.
            Parameter name: index
            at System.Collections.BitArray.Get(Int32 index)
            at Applica.Program.Main(String[] args) in C:\Documents and Settings\shampro\D
            esktop\Program.cs:line 55
            Press any key to continue . . .
            */

            using System;
            using System.IO;
            using System.Collections;
            using System.Collections.Generic;
            using System.Linq;
            using System.Text;

            namespace Applica
            {
            class Program
            {
            static void Main(string[] args)
            {
            DirectoryInfo da = new DirectoryInfo("C:\\Folder7");
            FileInfo[] Arr = da.GetFiles();
            if (Arr.Length == 0)
            {
            throw new InvalidOperationException("No files found.");
            }
            FileInfo ap = Arr[Arr.Length - 1];
            ulong Totbyte = (ulong)ap.Length;
            string filePath = ap.FullName;
            string temPath = Path.GetTempFileName();
            byte[] data = File.ReadAllBytes(filePath);
            File.WriteAllBytes(temPath, data);
            byte[] dataa = new byte[1];
            for (ulong counter = 0; counter < Totbyte; counter++)
            {
            dataa[0] = data[counter];
            BitArray bits = new BitArray(dataa);
            for (int count = 0; count < bits.Length;)
            {
            if (count != 7)
            {
            if (bits[count] == false && bits[count + 1] == true)
            count++;
            if (bits[count] == false && bits[count + 1] == false)
            count++;
            if (bits[count] == true && bits[count + 1] == true)/////line 55 error
            count++;
            if (bits[count] == true && bits[count + 1] == false)
            count++;
            Console.WriteLine("count = {0}", count);
            }
            }
            }
            }

            Richard DeemingR Offline
            Richard DeemingR Offline
            Richard Deeming
            wrote on last edited by
            #5

            You're incrementing the count variable within the loop, and then trying to access the item at the new incremented index without checking whether it's in range. Once the count is out of range, the indexer will throw an IndexOutOfRangeException. Add a test to the start of each if block to ensure that the index is still in range:

            if (count < bits.Length - 1 && bits[count] == false && bits[count + 1] == true)
            count++;

            if (count < bits.Length - 1 && bits[count] == false && bits[count + 1] == false)
            count++;

            if (count < bits.Length - 1 && bits[count] == true && bits[count + 1] == true)
            count++;

            if (count < bits.Length - 1 && bits[count] == true && bits[count + 1] == false)
            count++;


            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

            "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

            C 1 Reply Last reply
            0
            • Richard DeemingR Richard Deeming

              You're incrementing the count variable within the loop, and then trying to access the item at the new incremented index without checking whether it's in range. Once the count is out of range, the indexer will throw an IndexOutOfRangeException. Add a test to the start of each if block to ensure that the index is still in range:

              if (count < bits.Length - 1 && bits[count] == false && bits[count + 1] == true)
              count++;

              if (count < bits.Length - 1 && bits[count] == false && bits[count + 1] == false)
              count++;

              if (count < bits.Length - 1 && bits[count] == true && bits[count + 1] == true)
              count++;

              if (count < bits.Length - 1 && bits[count] == true && bits[count + 1] == false)
              count++;


              "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

              C Offline
              C Offline
              computerpublic
              wrote on last edited by
              #6

              Your solution did not produce an error, but it I still cannot understand why it allowed to process the ENTIRE alphabet in one shot. It would not even allow me to process one(1) special character with producing an error. Can you please explain why this? I simply cannot wrap my thoughts around it.

              Richard DeemingR 1 Reply Last reply
              0
              • C computerpublic

                Your solution did not produce an error, but it I still cannot understand why it allowed to process the ENTIRE alphabet in one shot. It would not even allow me to process one(1) special character with producing an error. Can you please explain why this? I simply cannot wrap my thoughts around it.

                Richard DeemingR Offline
                Richard DeemingR Offline
                Richard Deeming
                wrote on last edited by
                #7

                For ASCII characters, the byte values will be 127 or less. These bytes do not have the most significant bit set. For byte values greater than 127, the most significant bit is set. Due to the way you've constructed your loop, if the most-significant bit is set, you will increment count out of range within the loop, and without the range checks in place, you'll get an exception.


                "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                C 1 Reply Last reply
                0
                • C computerpublic

                  /*I have elimated some of the code in order to make it short and get straight to the point. I am noticing that my program will NOT process any of the non regular ascii characters (decimal 161 to 255 || hex A1 to FF) without the error below. All other regular ascii characters are process ok without errors.
                  OUTPUT::::::::::BELOW

                  count = 1
                  count = 3
                  count = 6

                  Unhandled Exception: System.ArgumentOutOfRangeException: Index was out of range.
                  Must be non-negative and less than the size of the collection.
                  Parameter name: index
                  at System.Collections.BitArray.Get(Int32 index)
                  at Applica.Program.Main(String[] args) in C:\Documents and Settings\shampro\D
                  esktop\Program.cs:line 55
                  Press any key to continue . . .
                  */

                  using System;
                  using System.IO;
                  using System.Collections;
                  using System.Collections.Generic;
                  using System.Linq;
                  using System.Text;

                  namespace Applica
                  {
                  class Program
                  {
                  static void Main(string[] args)
                  {
                  DirectoryInfo da = new DirectoryInfo("C:\\Folder7");
                  FileInfo[] Arr = da.GetFiles();
                  if (Arr.Length == 0)
                  {
                  throw new InvalidOperationException("No files found.");
                  }
                  FileInfo ap = Arr[Arr.Length - 1];
                  ulong Totbyte = (ulong)ap.Length;
                  string filePath = ap.FullName;
                  string temPath = Path.GetTempFileName();
                  byte[] data = File.ReadAllBytes(filePath);
                  File.WriteAllBytes(temPath, data);
                  byte[] dataa = new byte[1];
                  for (ulong counter = 0; counter < Totbyte; counter++)
                  {
                  dataa[0] = data[counter];
                  BitArray bits = new BitArray(dataa);
                  for (int count = 0; count < bits.Length;)
                  {
                  if (count != 7)
                  {
                  if (bits[count] == false && bits[count + 1] == true)
                  count++;
                  if (bits[count] == false && bits[count + 1] == false)
                  count++;
                  if (bits[count] == true && bits[count + 1] == true)/////line 55 error
                  count++;
                  if (bits[count] == true && bits[count + 1] == false)
                  count++;
                  Console.WriteLine("count = {0}", count);
                  }
                  }
                  }
                  }

                  B Offline
                  B Offline
                  BobJanova
                  wrote on last edited by
                  #8

                  The best part about this is that if you make it work, i.e. don't increment count before checking values again, the entire content of the if statement simplifies to

                  count++;
                  Console.WriteLine("count = {0}", count);

                  ... because you are testing all combinations. I'm not sure what you're trying to do but I'm sure the code is not doing it.

                  C Richard DeemingR 2 Replies Last reply
                  0
                  • Richard DeemingR Richard Deeming

                    For ASCII characters, the byte values will be 127 or less. These bytes do not have the most significant bit set. For byte values greater than 127, the most significant bit is set. Due to the way you've constructed your loop, if the most-significant bit is set, you will increment count out of range within the loop, and without the range checks in place, you'll get an exception.


                    "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                    C Offline
                    C Offline
                    computerpublic
                    wrote on last edited by
                    #9

                    I looked at my program for two (2) days straight. I looked at it night and day and could not make heads or tails of the problem. At one point I thought I was going to lose my mind. I am not sure I that I understand your explanation completely, but thanks. I really needed some sort of explanation so I put my mind at rest. Thanks.

                    1 Reply Last reply
                    0
                    • B BobJanova

                      The best part about this is that if you make it work, i.e. don't increment count before checking values again, the entire content of the if statement simplifies to

                      count++;
                      Console.WriteLine("count = {0}", count);

                      ... because you are testing all combinations. I'm not sure what you're trying to do but I'm sure the code is not doing it.

                      C Offline
                      C Offline
                      computerpublic
                      wrote on last edited by
                      #10

                      I was also reading online and saw that many people had this same problem. One of the experts said it was a dot.net error and that people should contact Microsoft tech support for a fix.

                      C 1 Reply Last reply
                      0
                      • C computerpublic

                        I was also reading online and saw that many people had this same problem. One of the experts said it was a dot.net error and that people should contact Microsoft tech support for a fix.

                        C Offline
                        C Offline
                        computerpublic
                        wrote on last edited by
                        #11

                        This is indeed a very strange problem to experience. I ran the entire English alphabet all at once and did not get an error. I ran only one special character and got errors every time.

                        1 Reply Last reply
                        0
                        • B BobJanova

                          The best part about this is that if you make it work, i.e. don't increment count before checking values again, the entire content of the if statement simplifies to

                          count++;
                          Console.WriteLine("count = {0}", count);

                          ... because you are testing all combinations. I'm not sure what you're trying to do but I'm sure the code is not doing it.

                          Richard DeemingR Offline
                          Richard DeemingR Offline
                          Richard Deeming
                          wrote on last edited by
                          #12

                          That's what I thought at first, but it's not quite that simple. If condition 1 is met, you move to the next index, and test condition 2 against the new index. Therefore, it's possible to increment the indexer at least three times within the same iteration of the loop. For example:

                          0 1 1 0
                          ^

                          (condition 1 is met)

                          0 1 1 0
                          ^

                          (condition 2 is not met)
                          (condition 3 is met)

                          0 1 1 0
                          ^

                          (condition 4 is met)

                          0 1 1 0
                          ^


                          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                          "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                          B 1 Reply Last reply
                          0
                          • Richard DeemingR Richard Deeming

                            That's what I thought at first, but it's not quite that simple. If condition 1 is met, you move to the next index, and test condition 2 against the new index. Therefore, it's possible to increment the indexer at least three times within the same iteration of the loop. For example:

                            0 1 1 0
                            ^

                            (condition 1 is met)

                            0 1 1 0
                            ^

                            (condition 2 is not met)
                            (condition 3 is met)

                            0 1 1 0
                            ^

                            (condition 4 is met)

                            0 1 1 0
                            ^


                            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                            B Offline
                            B Offline
                            BobJanova
                            wrote on last edited by
                            #13

                            That's true as it stands but I'm pretty sure that's not what's intended, because incrementing count multiple times can easily result in an out of range exception.

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups