apply foreach in a byte[] ArrayList
-
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) { }
-
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) { }
-
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) { }
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
-
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) { }
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.
-
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.
-
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
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.
-
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.
-
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
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; } }
-
, it's deep inside me to allways do an '!=null' check after 'as'. :-D
All the best, Martin
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.
-
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.
-
CHeck my last reply Works Perfect now... time to rest :zzz:
-
CHeck my last reply Works Perfect now... time to rest :zzz: