473,608 Members | 2,074 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 1895

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

Similar topics

3
4564
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 stopped me in my tracks. I must have used FileStream.Read a thousand times and it never dawned on me that I never have to specify either an "out" or "ref" when I pass the byte array that I pass to this function- yet it returns the bytes read from the...
2
7805
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 byte array and then call Loadfile method of RichTextBox and pass the stream. This works BUT IF I have more records I have to create a loop and some how pass the concatenated byte array and then pass it in a stream. I do this with
2
2249
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 far, it works great (after having a bit of heartache about .Dispose() ). Now, I need a certain part of the class file to run through many times and
6
17141
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 stating it's not a valid Com component. I'm new to vb.net so any help would be appreciated. Thanks. -- Dennis in Houston
6
2762
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) using redim arrays to add to a normal byte array (2) using an ArrayList and finally (3) using a memorystream. These three classes are listed below the test sub called "TestBuildByteArray". It was interesting that using the memorystream was...
32
6352
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 the value of that individual byte, if that byte is what I want, then I insert it into the new byte(). How can I do it? -- Kind regards
6
1816
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 arraylist so far, but i either get a cast exception or the value doesn't seem to get updated at all. What should i be doing to make it work? Thanks in advance Anders
3
9850
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 problem I am having is that although the arrays are being properly stored in the ArrayList, I cannot access the individual items in the arrays. For instance I was hoping to do this: public struct S { public int X;
1
2438
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 of different lengths (e.g. usually between 25 and 45 rows. The columns in each file have the same length). The text files have been numbered sequentially e.g. cb0, cb1, cb2 and so on. I would like to read the data from each text file into...
0
8087
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8025
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8493
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8365
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5499
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3993
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4053
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2493
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1620
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.