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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. apply foreach in a byte[] ArrayList

apply foreach in a byte[] ArrayList

Scheduled Pinned Locked Moved C#
tutorialquestion
12 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.
  • P Patricio Tapia

    Hi. an example: byte[] fileconte = new byte[6]; (code and code....) ArrayList list = new ArrayList(); list.Add(fileconte); How i can use a foreach to get list objets into a byte[]?? like: foreach (byte[] cont in list) { }

    J Offline
    J Offline
    JoeSharp
    wrote on last edited by
    #2

    hi byte[] byteArray = (byte[])list.ToArray(typeof(byte)); regards

    1 Reply Last reply
    0
    • P Patricio Tapia

      Hi. an example: byte[] fileconte = new byte[6]; (code and code....) ArrayList list = new ArrayList(); list.Add(fileconte); How i can use a foreach to get list objets into a byte[]?? like: foreach (byte[] cont in list) { }

      M Offline
      M Offline
      Martin 0
      wrote on last edited by
      #3

      Hello, This will work if you only Add "byte[]"'s to your List. byte[] actArray1 = new byte[5]; byte[] actArray2 = new byte[6]; ArrayList actList = new ArrayList(); actList.Add(actArray1); actList.Add(actArray2); foreach(byte[] actArray in actList) { int length = actArray.Length; } But will throw an exception (System.InvalidCastException') if you also add different types to the List: byte[] actArray1 = new byte[5]; byte[] actArray2 = new byte[6]; Control actControl = new Control(); ArrayList actList = new ArrayList(); actList.Add(actArray1); actList.Add(actArray2); actList.Add(actControl); foreach(byte[] actArray in actList) //will throw the exception here { int length = actArray.Length; } There for you could use: byte[] actArray1 = new byte[5]; byte[] actArray2 = new byte[6]; Control actControl = new Control(); ArrayList actList = new ArrayList(); actList.Add(actArray1); actList.Add(actArray2); actList.Add(actControl); foreach(object actObject in actList) { byte[] actArray = actObject as byte[]; if(actArray!=null) { int length = actArray.Length; } } IF you are using framework > .Net1.1, you could use a generic list.

      All the best, Martin

      P 1 Reply Last reply
      0
      • P Patricio Tapia

        Hi. an example: byte[] fileconte = new byte[6]; (code and code....) ArrayList list = new ArrayList(); list.Add(fileconte); How i can use a foreach to get list objets into a byte[]?? like: foreach (byte[] cont in list) { }

        CPalliniC Offline
        CPalliniC Offline
        CPallini
        wrote on last edited by
        #4

        this?

        byte[] fileconte = new byte[6];
        //...
        ArrayList list = new ArrayList();
        list.Add(fileconte);
        //How i can use a foreach to get list objets into a byte[]?? like:
        foreach (object cont in list) {
        byte[] myByteArray = cont as byte[];
        myByteArray[0] = 5;
        }

        Cheers :)

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

        In testa che avete, signor di Ceprano?

        M 1 Reply Last reply
        0
        • CPalliniC CPallini

          this?

          byte[] fileconte = new byte[6];
          //...
          ArrayList list = new ArrayList();
          list.Add(fileconte);
          //How i can use a foreach to get list objets into a byte[]?? like:
          foreach (object cont in list) {
          byte[] myByteArray = cont as byte[];
          myByteArray[0] = 5;
          }

          Cheers :)

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

          M Offline
          M Offline
          Martin 0
          wrote on last edited by
          #5

          Hello,

          CPallini wrote:

          byte[] myByteArray = cont as byte[];

          As the 'as' could also return 'null',

          CPallini wrote:

          myByteArray[0] = 5;

          this could leed to an null ref exception!

          All the best, Martin

          CPalliniC 1 Reply Last reply
          0
          • M Martin 0

            Hello,

            CPallini wrote:

            byte[] myByteArray = cont as byte[];

            As the 'as' could also return 'null',

            CPallini wrote:

            myByteArray[0] = 5;

            this could leed to an null ref exception!

            All the best, Martin

            CPalliniC Offline
            CPalliniC Offline
            CPallini
            wrote on last edited by
            #6

            Of course I know, but I leave all the checking code as an exercise for the reader. :-D BTW that's enforce flexibility as you may choose to: (1) Really write the checking code. (2) Use a precondition/postcondition approach on your project. (3) Simply thrust preceeding code (viable only for very small projects). Cheers :)

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

            In testa che avete, signor di Ceprano?

            M 1 Reply Last reply
            0
            • CPalliniC CPallini

              Of course I know, but I leave all the checking code as an exercise for the reader. :-D BTW that's enforce flexibility as you may choose to: (1) Really write the checking code. (2) Use a precondition/postcondition approach on your project. (3) Simply thrust preceeding code (viable only for very small projects). Cheers :)

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

              M Offline
              M Offline
              Martin 0
              wrote on last edited by
              #7

              , it's deep inside me to allways do an '!=null' check after 'as'. :-D

              All the best, Martin

              CPalliniC 1 Reply Last reply
              0
              • M Martin 0

                Hello, This will work if you only Add "byte[]"'s to your List. byte[] actArray1 = new byte[5]; byte[] actArray2 = new byte[6]; ArrayList actList = new ArrayList(); actList.Add(actArray1); actList.Add(actArray2); foreach(byte[] actArray in actList) { int length = actArray.Length; } But will throw an exception (System.InvalidCastException') if you also add different types to the List: byte[] actArray1 = new byte[5]; byte[] actArray2 = new byte[6]; Control actControl = new Control(); ArrayList actList = new ArrayList(); actList.Add(actArray1); actList.Add(actArray2); actList.Add(actControl); foreach(byte[] actArray in actList) //will throw the exception here { int length = actArray.Length; } There for you could use: byte[] actArray1 = new byte[5]; byte[] actArray2 = new byte[6]; Control actControl = new Control(); ArrayList actList = new ArrayList(); actList.Add(actArray1); actList.Add(actArray2); actList.Add(actControl); foreach(object actObject in actList) { byte[] actArray = actObject as byte[]; if(actArray!=null) { int length = actArray.Length; } } IF you are using framework > .Net1.1, you could use a generic list.

                All the best, Martin

                P Offline
                P Offline
                Patricio Tapia
                wrote on last edited by
                #8

                Thanks Now i'm remember that byte in ArrayList This is my code: class Program { static void Main(string[] args) { Program a = new Program(); byte[] actArray1 = new byte[5]; byte[] actArray2 = new byte[5]; byte[] cant = new byte[5]; ArrayList actList = new ArrayList(); actArray1[0] = 0x6f; actArray1[1] = 0x6f; actArray1[2] = 0x5f; actArray1[3] = 0x6f; actArray1[4] = 0x1f; actArray2[0] = 0x6a; actArray2[1] = 0x6f; actArray2[2] = 0x6f; actArray2[3] = 0x6a; actArray2[4] = 0x4f; cant[0] = 0x6a; cant[1] = 0x6f; cant[2] = 0x6f; cant[3] = 0x6a; cant[4] = 0x4f; actList.Add(actArray1); actList.Add(actArray2); foreach (byte[] actArray in actList) { if (a.AreEqual(actArray,cant)) Console.WriteLine("same byte array"); } Console.ReadLine(); } private bool AreEqual(byte[] a, byte[] b) { if (a.Length != b.Length) return false; for (int i = 0; i < a.Length; i++) if (a[i] != b[i]) return false; return true; } }

                1 Reply Last reply
                0
                • M Martin 0

                  , it's deep inside me to allways do an '!=null' check after 'as'. :-D

                  All the best, Martin

                  CPalliniC Offline
                  CPalliniC Offline
                  CPallini
                  wrote on last edited by
                  #9

                  Because you got married to strategy no.1 :-D It's a safe marriage: ALL THE BEST! :)

                  If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

                  In testa che avete, signor di Ceprano?

                  M 1 Reply Last reply
                  0
                  • CPalliniC CPallini

                    Because you got married to strategy no.1 :-D It's a safe marriage: ALL THE BEST! :)

                    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

                    M Offline
                    M Offline
                    Martin 0
                    wrote on last edited by
                    #10

                    :rose: :-D

                    All the best, Martin

                    P 1 Reply Last reply
                    0
                    • M Martin 0

                      :rose: :-D

                      All the best, Martin

                      P Offline
                      P Offline
                      Patricio Tapia
                      wrote on last edited by
                      #11

                      CHeck my last reply Works Perfect now... time to rest :zzz:

                      M 1 Reply Last reply
                      0
                      • P Patricio Tapia

                        CHeck my last reply Works Perfect now... time to rest :zzz:

                        M Offline
                        M Offline
                        Martin 0
                        wrote on last edited by
                        #12

                        I'm happy for you! sleep well!

                        All the best, Martin

                        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