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. Invalid Argument in For Loop

Invalid Argument in For Loop

Scheduled Pinned Locked Moved C#
csharplinqhelpquestion
24 Posts 6 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 computerpublic

    /* I am getting an invalid argument error in my for loop and I don't understand why */
    using System;
    using System.IO;
    using System.Collections;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Boolean;

    namespace Applica
    {
    class Program
    {
    static void Main(string[] args)
    {
    DirectoryInfo da = new DirectoryInfo("C:\\Folder9");
    FileInfo[] Arr = da.GetFiles();
    FileInfo ap = Arr[Arr.Length - 1];
    long Totbyte = ap.Length;
    string filePath = ap.FullName;
    string temPath = Path.GetTempFileName();
    byte[] data = File.ReadAllBytes(filePath);
    File.WriteAllBytes(temPath, data);
    byte[] dataB = new byte[1];
    BitArray bits = new BitArray(dataB);
    for (uint counter = 0; counter < Totbyte; counter++)
    {
    dataB[0] = data[counter];
    for (uint count = 0; count < bits.Length; count++)
    {
    if (bits[count] == 0)
    Console.Write(bits[count] ? "1" : "0");
    }
    }
    }
    }
    }

    P Offline
    P Offline
    Pete OHanlon
    wrote on last edited by
    #2

    Totbyte is a long. Your loop is a uint.

    C 2 Replies Last reply
    0
    • P Pete OHanlon

      Totbyte is a long. Your loop is a uint.

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

      Even if I change that variable to a constant, the error still exist.

      1 Reply Last reply
      0
      • P Pete OHanlon

        Totbyte is a long. Your loop is a uint.

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

        My error is in the last 2 lines of the code:

        bits[count]

        1 Reply Last reply
        0
        • C computerpublic

          /* I am getting an invalid argument error in my for loop and I don't understand why */
          using System;
          using System.IO;
          using System.Collections;
          using System.Collections.Generic;
          using System.Linq;
          using System.Text;
          using System.Boolean;

          namespace Applica
          {
          class Program
          {
          static void Main(string[] args)
          {
          DirectoryInfo da = new DirectoryInfo("C:\\Folder9");
          FileInfo[] Arr = da.GetFiles();
          FileInfo ap = Arr[Arr.Length - 1];
          long Totbyte = ap.Length;
          string filePath = ap.FullName;
          string temPath = Path.GetTempFileName();
          byte[] data = File.ReadAllBytes(filePath);
          File.WriteAllBytes(temPath, data);
          byte[] dataB = new byte[1];
          BitArray bits = new BitArray(dataB);
          for (uint counter = 0; counter < Totbyte; counter++)
          {
          dataB[0] = data[counter];
          for (uint count = 0; count < bits.Length; count++)
          {
          if (bits[count] == 0)
          Console.Write(bits[count] ? "1" : "0");
          }
          }
          }
          }
          }

          P Offline
          P Offline
          PIEBALDconsult
          wrote on last edited by
          #5

          An array index has to be an int (or something implicitly convertable to an int). http://msdn.microsoft.com/en-us/library/system.collections.ilist.item(v=vs.110).aspx[^]

          This space intentionally left blank.

          C 1 Reply Last reply
          0
          • P PIEBALDconsult

            An array index has to be an int (or something implicitly convertable to an int). http://msdn.microsoft.com/en-us/library/system.collections.ilist.item(v=vs.110).aspx[^]

            This space intentionally left blank.

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

            I changed it to an int and I still get an error.

            L 1 Reply Last reply
            0
            • C computerpublic

              I changed it to an int and I still get an error.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #7

              The same one or a new one?

              Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

              C 1 Reply Last reply
              0
              • L Lost User

                The same one or a new one?

                Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

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

                Hey Guy (Eddy Vluggen), I am trying to learn how to program and you continually send insults to me. I am asking you nicely to be respectful and don't respond to me. You are obviously a very miserable person who can find nothing better to do than insult people are asking for help. Why are you even in the forum if you contribute no help. Is this the highlight of your day? Dude you must really need a hug.

                L 1 Reply Last reply
                0
                • C computerpublic

                  /* I am getting an invalid argument error in my for loop and I don't understand why */
                  using System;
                  using System.IO;
                  using System.Collections;
                  using System.Collections.Generic;
                  using System.Linq;
                  using System.Text;
                  using System.Boolean;

                  namespace Applica
                  {
                  class Program
                  {
                  static void Main(string[] args)
                  {
                  DirectoryInfo da = new DirectoryInfo("C:\\Folder9");
                  FileInfo[] Arr = da.GetFiles();
                  FileInfo ap = Arr[Arr.Length - 1];
                  long Totbyte = ap.Length;
                  string filePath = ap.FullName;
                  string temPath = Path.GetTempFileName();
                  byte[] data = File.ReadAllBytes(filePath);
                  File.WriteAllBytes(temPath, data);
                  byte[] dataB = new byte[1];
                  BitArray bits = new BitArray(dataB);
                  for (uint counter = 0; counter < Totbyte; counter++)
                  {
                  dataB[0] = data[counter];
                  for (uint count = 0; count < bits.Length; count++)
                  {
                  if (bits[count] == 0)
                  Console.Write(bits[count] ? "1" : "0");
                  }
                  }
                  }
                  }
                  }

                  N Offline
                  N Offline
                  noone2407
                  wrote on last edited by
                  #9

                  Can you comment all your code? Then I will fix it for you. PS: always check variable is null or not, if you are not sure before calling it

                  C 1 Reply Last reply
                  0
                  • N noone2407

                    Can you comment all your code? Then I will fix it for you. PS: always check variable is null or not, if you are not sure before calling it

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

                    <pre>
                    using System;
                    using System.IO;
                    using System.Collections;
                    using System.Collections.Generic;
                    using System.Linq;
                    using System.Text;
                    using System.Boolean;

                    namespace Applica
                    {
                    class Program
                    {
                    static void Main(string[] args)
                    {
                    DirectoryInfo da = new DirectoryInfo("C:\\Folder9");
                    FileInfo[] Arr = da.GetFiles();
                    FileInfo ap = Arr[Arr.Length - 1];
                    long Totbyte = ap.Length;//file length
                    string filePath = ap.FullName;//file name
                    string temPath = Path.GetTempFileName();//path
                    byte[] data = File.ReadAllBytes(filePath);//reading entire file
                    File.WriteAllBytes(temPath, data); //writing entire file to byte array
                    byte[] dataB = new byte[1];//declaring a one element byte array
                    BitArray bits = new BitArray(dataB);//moving one byte to a bit array (8 bits)
                    for (uint counter = 0; counter < Totbyte; counter++)
                    {
                    dataB[0] = data[counter];//moving data one byte at time.
                    for (uint count = 0; count < bits.Length; count++)//looking at each bit of each byte
                    {
                    if (bits[count] == 0)//PROBLEM AREA
                    Console.Write(bits[count] ? "1" : "0");
                    }
                    }
                    }
                    }
                    }

                    N 1 Reply Last reply
                    0
                    • C computerpublic

                      <pre>
                      using System;
                      using System.IO;
                      using System.Collections;
                      using System.Collections.Generic;
                      using System.Linq;
                      using System.Text;
                      using System.Boolean;

                      namespace Applica
                      {
                      class Program
                      {
                      static void Main(string[] args)
                      {
                      DirectoryInfo da = new DirectoryInfo("C:\\Folder9");
                      FileInfo[] Arr = da.GetFiles();
                      FileInfo ap = Arr[Arr.Length - 1];
                      long Totbyte = ap.Length;//file length
                      string filePath = ap.FullName;//file name
                      string temPath = Path.GetTempFileName();//path
                      byte[] data = File.ReadAllBytes(filePath);//reading entire file
                      File.WriteAllBytes(temPath, data); //writing entire file to byte array
                      byte[] dataB = new byte[1];//declaring a one element byte array
                      BitArray bits = new BitArray(dataB);//moving one byte to a bit array (8 bits)
                      for (uint counter = 0; counter < Totbyte; counter++)
                      {
                      dataB[0] = data[counter];//moving data one byte at time.
                      for (uint count = 0; count < bits.Length; count++)//looking at each bit of each byte
                      {
                      if (bits[count] == 0)//PROBLEM AREA
                      Console.Write(bits[count] ? "1" : "0");
                      }
                      }
                      }
                      }
                      }

                      N Offline
                      N Offline
                      noone2407
                      wrote on last edited by
                      #11

                      I don't understand what you are trying to do. But if you want to write out the byte array of the file then here is the code

                              DirectoryInfo da = new DirectoryInfo("C:\\\\Folder9");
                              FileInfo\[\] Arr = da.GetFiles();
                              FileInfo ap = Arr\[Arr.Length - 1\];
                              long Totbyte = ap.Length;//file length
                              string filePath = ap.FullName;//file name
                              byte\[\] data = File.ReadAllBytes(filePath);//reading entire file
                              for (int counter = 0; counter < Totbyte; counter++)
                              {
                      			Console.Write(data\[counter\]);
                              }
                      

                      Remember that is byte array, not bit array If you to write out bit array instead of byte array, replace

                      Console.Write(data[counter]);

                      Into

                      string yourByteString = Convert.ToString(data[counter], 2).PadLeft(8, '0');
                      Console.Write(yourByteString+" ");

                      1 Reply Last reply
                      0
                      • C computerpublic

                        Hey Guy (Eddy Vluggen), I am trying to learn how to program and you continually send insults to me. I am asking you nicely to be respectful and don't respond to me. You are obviously a very miserable person who can find nothing better to do than insult people are asking for help. Why are you even in the forum if you contribute no help. Is this the highlight of your day? Dude you must really need a hug.

                        L Offline
                        L Offline
                        Lost User
                        wrote on last edited by
                        #12

                        He did not insult you, he asked you a perfectly reasonable question. You have stated that you get an error, but you have given no details of what that error is. Is it "invalid argument", and if so on what line of code. If it is something different then show the details. But in either case please be clear what the problem is and where it occurs, and people will try to help you. But if you resort to insults and rudeness you are likely to get no help at all.

                        1 Reply Last reply
                        0
                        • C computerpublic

                          /* I am getting an invalid argument error in my for loop and I don't understand why */
                          using System;
                          using System.IO;
                          using System.Collections;
                          using System.Collections.Generic;
                          using System.Linq;
                          using System.Text;
                          using System.Boolean;

                          namespace Applica
                          {
                          class Program
                          {
                          static void Main(string[] args)
                          {
                          DirectoryInfo da = new DirectoryInfo("C:\\Folder9");
                          FileInfo[] Arr = da.GetFiles();
                          FileInfo ap = Arr[Arr.Length - 1];
                          long Totbyte = ap.Length;
                          string filePath = ap.FullName;
                          string temPath = Path.GetTempFileName();
                          byte[] data = File.ReadAllBytes(filePath);
                          File.WriteAllBytes(temPath, data);
                          byte[] dataB = new byte[1];
                          BitArray bits = new BitArray(dataB);
                          for (uint counter = 0; counter < Totbyte; counter++)
                          {
                          dataB[0] = data[counter];
                          for (uint count = 0; count < bits.Length; count++)
                          {
                          if (bits[count] == 0)
                          Console.Write(bits[count] ? "1" : "0");
                          }
                          }
                          }
                          }
                          }

                          OriginalGriffO Offline
                          OriginalGriffO Offline
                          OriginalGriff
                          wrote on last edited by
                          #13

                          There are two problems here: firstly that an array index needs to be an integer - and a uint needs an explicit cast to int - you can't use an implicit cast because it could "throw away" data in the form of positive values with the top bit set. Easiest solution: use int value in your for loop. The second problem is that the BitArray indexer does not return an int value: it returns a bool. So if you correct the first error, the compiler will complain that it cannot compare an int with a bool! Those are easily fixed to let your program compile:

                          for (int counter = 0; counter < Totbyte; counter++)
                          {
                          dataB[0] = data[counter];
                          for (int count = 0; count < bits.Length; count++)
                          {
                          if (!bits[count])
                          Console.Write(bits[count] ? "1" : "0");
                          }
                          }

                          But... It's not going to work. You don't change the value in Bits at all inside your outer loop - so each byte "value" is going to print as the same sequence of bits. Personally, I wouldn't use a BitArray - it's an unnecessary complication here - just use the C# standard bit manipulation operators:

                          for (int counter = 0; counter < Totbyte; counter++)
                          {
                          int b = (int) data[counter];
                          for (int count = 0; count < 8; count++)
                          {
                          Console.Write(b & 1);
                          b = b >> 1;
                          }
                          }

                          Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952) Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)

                          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                          "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                          C 1 Reply Last reply
                          0
                          • C computerpublic

                            /* I am getting an invalid argument error in my for loop and I don't understand why */
                            using System;
                            using System.IO;
                            using System.Collections;
                            using System.Collections.Generic;
                            using System.Linq;
                            using System.Text;
                            using System.Boolean;

                            namespace Applica
                            {
                            class Program
                            {
                            static void Main(string[] args)
                            {
                            DirectoryInfo da = new DirectoryInfo("C:\\Folder9");
                            FileInfo[] Arr = da.GetFiles();
                            FileInfo ap = Arr[Arr.Length - 1];
                            long Totbyte = ap.Length;
                            string filePath = ap.FullName;
                            string temPath = Path.GetTempFileName();
                            byte[] data = File.ReadAllBytes(filePath);
                            File.WriteAllBytes(temPath, data);
                            byte[] dataB = new byte[1];
                            BitArray bits = new BitArray(dataB);
                            for (uint counter = 0; counter < Totbyte; counter++)
                            {
                            dataB[0] = data[counter];
                            for (uint count = 0; count < bits.Length; count++)
                            {
                            if (bits[count] == 0)
                            Console.Write(bits[count] ? "1" : "0");
                            }
                            }
                            }
                            }
                            }

                            L Offline
                            L Offline
                            Lost User
                            wrote on last edited by
                            #14

                            You cannot use an unsigned as an index, it must be signed. And you cannot compare a bit with the integer value zero. A bit can only be true or false. Change the loop code to

                            for (int count = 0; count < bits.Length; count++)
                            {
                            if (bits[count])
                            Console.Write(bits[count] ? "1" : "0");
                            }

                            C 1 Reply Last reply
                            0
                            • OriginalGriffO OriginalGriff

                              There are two problems here: firstly that an array index needs to be an integer - and a uint needs an explicit cast to int - you can't use an implicit cast because it could "throw away" data in the form of positive values with the top bit set. Easiest solution: use int value in your for loop. The second problem is that the BitArray indexer does not return an int value: it returns a bool. So if you correct the first error, the compiler will complain that it cannot compare an int with a bool! Those are easily fixed to let your program compile:

                              for (int counter = 0; counter < Totbyte; counter++)
                              {
                              dataB[0] = data[counter];
                              for (int count = 0; count < bits.Length; count++)
                              {
                              if (!bits[count])
                              Console.Write(bits[count] ? "1" : "0");
                              }
                              }

                              But... It's not going to work. You don't change the value in Bits at all inside your outer loop - so each byte "value" is going to print as the same sequence of bits. Personally, I wouldn't use a BitArray - it's an unnecessary complication here - just use the C# standard bit manipulation operators:

                              for (int counter = 0; counter < Totbyte; counter++)
                              {
                              int b = (int) data[counter];
                              for (int count = 0; count < 8; count++)
                              {
                              Console.Write(b & 1);
                              b = b >> 1;
                              }
                              }

                              Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952) Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)

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

                              I only need to evaluate if whether or not a "0" exist in the BitArray string. I dont care about the "1".

                              OriginalGriffO 1 Reply Last reply
                              0
                              • L Lost User

                                You cannot use an unsigned as an index, it must be signed. And you cannot compare a bit with the integer value zero. A bit can only be true or false. Change the loop code to

                                for (int count = 0; count < bits.Length; count++)
                                {
                                if (bits[count])
                                Console.Write(bits[count] ? "1" : "0");
                                }

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

                                I only need to evaluate if whether or not a "0" exist in the BitArray string. I dont care about the "1".

                                L 1 Reply Last reply
                                0
                                • C computerpublic

                                  I only need to evaluate if whether or not a "0" exist in the BitArray string. I dont care about the "1".

                                  L Offline
                                  L Offline
                                  Lost User
                                  wrote on last edited by
                                  #17

                                  Then just test for false; the BitArray[item] is a boolean value.

                                  C 1 Reply Last reply
                                  0
                                  • L Lost User

                                    Then just test for false; the BitArray[item] is a boolean value.

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

                                    Should it be: bool a = false; a=bits[count]; if (a) { } is this correct?

                                    L 1 Reply Last reply
                                    0
                                    • C computerpublic

                                      Should it be: bool a = false; a=bits[count]; if (a) { } is this correct?

                                      L Offline
                                      L Offline
                                      Lost User
                                      wrote on last edited by
                                      #19

                                      No, you should just be testing the specific bit:

                                      if (!bits[count])
                                      // OR
                                      if (bits[count] == false)

                                      However, having looked more closely at your code I am at a loss to understand what the program is supposed to do. In particular why you are reading those files and why you are writing new copies of them, or what any of that has to do with your for loops.

                                      C 1 Reply Last reply
                                      0
                                      • C computerpublic

                                        I only need to evaluate if whether or not a "0" exist in the BitArray string. I dont care about the "1".

                                        OriginalGriffO Offline
                                        OriginalGriffO Offline
                                        OriginalGriff
                                        wrote on last edited by
                                        #20

                                        And what are you going to do if it is a one? now print anything? :laugh: Trust me: AND the value with 1 returns either 0 or 1 depending on the state of teh least significant bit the in value. Using >> then shifts the whole input down a single bit, so the second bit takes the place of first, and so forth. It's a lot more efficient than using a BitString for each byte.

                                        Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952) Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)

                                        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                                        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                                        1 Reply Last reply
                                        0
                                        • L Lost User

                                          No, you should just be testing the specific bit:

                                          if (!bits[count])
                                          // OR
                                          if (bits[count] == false)

                                          However, having looked more closely at your code I am at a loss to understand what the program is supposed to do. In particular why you are reading those files and why you are writing new copies of them, or what any of that has to do with your for loops.

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

                                          /*No more errors. My input to the program is letter "e" which is enclosed within a text file in c:\\folder9. The decimal equivalent of letter "e" is 101. When I run 101 thru the BitArray, it evaluates all bits as false which is NOT CORRECT.*/
                                          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:\\Folder9");
                                          FileInfo[] Arr = da.GetFiles();
                                          if (Arr.Length == 0)
                                          {
                                          throw new InvalidOperationException("No files found.");
                                          }
                                          // No need to loop through the array just to get the last item:
                                          FileInfo ap = Arr[Arr.Length - 1];
                                          long Totbyte = ap.Length;
                                          string filePath = ap.FullName;
                                          Console.WriteLine("Total Bytes = {0} bytes", Totbyte);
                                          // GetTempFileName *creates* the file, so it always exists:
                                          string temPath = Path.GetTempFileName();
                                          byte[] data = File.ReadAllBytes(filePath);
                                          File.WriteAllBytes(temPath, data);

                                                  byte\[\] dataB = new byte\[1\];
                                                  BitArray bits = new BitArray(dataB);
                                                  Console.WriteLine("length= {0}",bits.Length);
                                                  for (long counter = 0; counter < Totbyte; counter++)
                                                  {
                                                      dataB\[0\] = data\[counter\];
                                                      Console.WriteLine("{0}", dataB\[0\]);
                                                      for (int count = 0; count < bits.Length; count++)
                                                      {
                                                          if (bits\[count\] == true)
                                                              Console.WriteLine("{0}", bits\[count\]);
                                                          if (bits\[count\] == false)
                                                              Console.WriteLine("{0}", bits\[count\]);
                                                      }
                                                  }
                                              }
                                          }
                                          

                                          }

                                          L 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