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.
  • 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
    • C computerpublic

      /*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 Offline
      L Offline
      Lost User
      wrote on last edited by
      #22

      computerpublic wrote:

      When I run 101 thru the BitArray, it evaluates all bits as false which is NOT CORRECT

      But the problem is that you do not run that value through it. Your code is:

              byte\[\] dataB = new byte\[1\];
              // set bits to the content of dataB, which is null at this point
              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\]);
                  // bits still does not contain any value
                  for (int count = 0; count < bits.Length; count++)
                  {
                      // this can be simplified
                      if (bits\[count\] == true)
                          Console.WriteLine("{0}", bits\[count\]);
                      if (bits\[count\] == false)
                          Console.WriteLine("{0}", bits\[count\]);
                  }
              }
      

      It should be

              byte\[\] dataB = new byte\[1\];
              for (long counter = 0; counter < Totbyte; counter++)
              {
                  dataB\[0\] = data\[counter\];
                  BitArray bits = new BitArray(dataB);
                  Console.WriteLine("length= {0}",bits.Length);
                  Console.WriteLine("{0}", dataB\[0\]);
                  for (int count = 0; count < bits.Length; count++)
                  {
                      Console.Write(bits\[count\] ? "1" : "0");
                  }
                  Console.WriteLine("");
              }
      
      C 1 Reply Last reply
      0
      • L Lost User

        computerpublic wrote:

        When I run 101 thru the BitArray, it evaluates all bits as false which is NOT CORRECT

        But the problem is that you do not run that value through it. Your code is:

                byte\[\] dataB = new byte\[1\];
                // set bits to the content of dataB, which is null at this point
                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\]);
                    // bits still does not contain any value
                    for (int count = 0; count < bits.Length; count++)
                    {
                        // this can be simplified
                        if (bits\[count\] == true)
                            Console.WriteLine("{0}", bits\[count\]);
                        if (bits\[count\] == false)
                            Console.WriteLine("{0}", bits\[count\]);
                    }
                }
        

        It should be

                byte\[\] dataB = new byte\[1\];
                for (long counter = 0; counter < Totbyte; counter++)
                {
                    dataB\[0\] = data\[counter\];
                    BitArray bits = new BitArray(dataB);
                    Console.WriteLine("length= {0}",bits.Length);
                    Console.WriteLine("{0}", dataB\[0\]);
                    for (int count = 0; count < bits.Length; count++)
                    {
                        Console.Write(bits\[count\] ? "1" : "0");
                    }
                    Console.WriteLine("");
                }
        
        C Offline
        C Offline
        computerpublic
        wrote on last edited by
        #23

        I am noticing that the programs default operation is to evaluate the Most Significant Bit First and it evaluates the Least Significant Bit Last. I was not expecting this behavior. This this always the case the default case? Can I change the default operation to do LSB to MSB?

        L 1 Reply Last reply
        0
        • C computerpublic

          I am noticing that the programs default operation is to evaluate the Most Significant Bit First and it evaluates the Least Significant Bit Last. I was not expecting this behavior. This this always the case the default case? Can I change the default operation to do LSB to MSB?

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

          See http://msdn.microsoft.com/en-us/library/x1xda43a(v=vs.110).aspx[^].

          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