473,499 Members | 1,889 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Adding byte arrays to an ArrayList

25 New Member
When I add a series of byte arrays to an array list, then I read them back, all the arraylist byte array elements are identical to the last byte array entry. What is wrong? The following code will reproduce the problem.

Thanks,

Jon Jacobs

Expand|Select|Wrap|Line Numbers
  1. Imports System.Text
  2. Public Class Form1
  3.  
  4.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  5.         Dim a(9) As Byte
  6.         Dim al As ArrayList = New ArrayList
  7.  
  8.         Dim L As Integer
  9.         Dim total As Integer = 0
  10.         For i As Integer = 0 To 5
  11.             L = read(i, a)
  12.             total = total + L
  13.             al.Add(a) 'at this point the value of a is correct
  14.         Next
  15.         al.Add(L)
  16.         al.Add(total)
  17.  
  18.         Dim b0() As Byte = al(0)
  19.         Dim b1() As Byte = al(1)
  20.         Dim b2() As Byte = al(2)
  21.         Dim b3() As Byte = al(3)
  22.         Dim b4() As Byte = al(4)
  23.         Dim b5() As Byte = al(5)
  24.  
  25.         Dim result(54) As Byte ' = ArBldr(al)
  26.         Buffer.BlockCopy(b0, 0, result, 0, 10)
  27.         Buffer.BlockCopy(b1, 0, result, 10, 10)
  28.         Buffer.BlockCopy(b2, 0, result, 20, 10)
  29.         Buffer.BlockCopy(b3, 0, result, 30, 10)
  30.         Buffer.BlockCopy(b4, 0, result, 40, 10)
  31.         Buffer.BlockCopy(b5, 0, result, 50, 5)
  32.  
  33.         Dim S As String = Encoding.ASCII.GetString(result)
  34.  
  35.         MessageBox.Show(S)
  36.     End Sub
  37.  
  38.     Private Function read(ByVal index As Integer, ByVal b() As Byte) As Integer
  39.         Dim buf() As Byte
  40.         Select Case index
  41.             Case 0
  42.                 buf = Encoding.ASCII.GetBytes("AAAAAAAAAA")
  43.                 Buffer.BlockCopy(buf, 0, b, 0, buf.Length)
  44.                 Return buf.Length
  45.             Case 1
  46.                 buf = Encoding.ASCII.GetBytes("BBBBBBBBBB")
  47.                 Buffer.BlockCopy(buf, 0, b, 0, buf.Length)
  48.                 Return buf.Length
  49.             Case 2
  50.                 buf = Encoding.ASCII.GetBytes("CCCCCCCCCC")
  51.                 Buffer.BlockCopy(buf, 0, b, 0, buf.Length)
  52.                 Return buf.Length
  53.             Case 3
  54.                 buf = Encoding.ASCII.GetBytes("DDDDDDDDDD")
  55.                 Buffer.BlockCopy(buf, 0, b, 0, buf.Length)
  56.                 Return buf.Length
  57.             Case 4
  58.                 buf = Encoding.ASCII.GetBytes("EEEEEEEEEE")
  59.                 Buffer.BlockCopy(buf, 0, b, 0, buf.Length)
  60.                 Return buf.Length
  61.             Case 5
  62.                 buf = Encoding.ASCII.GetBytes("FFFFF")
  63.                 Buffer.BlockCopy(buf, 0, b, 0, buf.Length)
  64.                 Return buf.Length
  65.             Case Else
  66.                 Return 0
  67.         End Select
  68.     End Function
  69.  
  70. End Class
Aug 3 '07 #1
0 1860

Sign in to post your reply or Sign up for a free account.

Similar topics

3
4557
by: Gene | last post by:
I'm not sure I understand this behavior (at least in the context of C#). I understand the use of "ref" and "out" keywords as they apply to method arguments, and then something this morning...
2
7796
by: nitin8or | last post by:
Hi ALL, I want to display in a RichTextBox the Binary Large Objects data coming from database. If I have one record its not a problem I convert it to byte array and pass it on in a stream as a...
2
2235
by: David | last post by:
Hi all, I am fairly new to C#. so go easy on me :-) Anyhow, I have a class file that I have set up properties and a method. I am calling this class file directly from and aspx.cs file. So...
6
17132
by: Dennis | last post by:
I am trying to use ZLIB.Dll in a VB.Net project but keep getting an error message that says it can't load the DLL. I tried to add a reference using "Project-Add Reference" but get the error message...
6
2726
by: Dennis | last post by:
I was trying to determine the fastest way to build a byte array from components where the size of the individual components varied depending on the user's input. I tried three classes I built: (1)...
32
6304
by: Guoqi Zheng | last post by:
I am really do not know so much about byte/bit, etc. Question, if I defined a byte(), how can I add a single byte to it? what I want is that I have an array of bytes, I loop that array, look at...
6
1804
by: Anders Würtz | last post by:
i have an assignment to iterate through a collection containing different types of numeric values (float, double, int, byte, short etc.) and to add 1 to all of them. I tried with array and...
3
9842
by: Zenon | last post by:
I have a function which returns array of structs. I need to create a collection of those arrays and thought that an ArrayList would be a good way to do this since the count is variable. The...
1
2432
by: Doug_J_W | last post by:
I have a Visual Basic (2005) project that contains around twenty embedded text files as resources. The text files contain two columns of real numbers that are separated by tab deliminator, and are...
0
7134
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7012
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7180
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6901
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7392
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5479
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3105
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
667
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
307
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.