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 - Imports System.Text
-
Public Class Form1
-
-
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
-
Dim a(9) As Byte
-
Dim al As ArrayList = New ArrayList
-
-
Dim L As Integer
-
Dim total As Integer = 0
-
For i As Integer = 0 To 5
-
L = read(i, a)
-
total = total + L
-
al.Add(a) 'at this point the value of a is correct
-
Next
-
al.Add(L)
-
al.Add(total)
-
-
Dim b0() As Byte = al(0)
-
Dim b1() As Byte = al(1)
-
Dim b2() As Byte = al(2)
-
Dim b3() As Byte = al(3)
-
Dim b4() As Byte = al(4)
-
Dim b5() As Byte = al(5)
-
-
Dim result(54) As Byte ' = ArBldr(al)
-
Buffer.BlockCopy(b0, 0, result, 0, 10)
-
Buffer.BlockCopy(b1, 0, result, 10, 10)
-
Buffer.BlockCopy(b2, 0, result, 20, 10)
-
Buffer.BlockCopy(b3, 0, result, 30, 10)
-
Buffer.BlockCopy(b4, 0, result, 40, 10)
-
Buffer.BlockCopy(b5, 0, result, 50, 5)
-
-
Dim S As String = Encoding.ASCII.GetString(result)
-
-
MessageBox.Show(S)
-
End Sub
-
-
Private Function read(ByVal index As Integer, ByVal b() As Byte) As Integer
-
Dim buf() As Byte
-
Select Case index
-
Case 0
-
buf = Encoding.ASCII.GetBytes("AAAAAAAAAA")
-
Buffer.BlockCopy(buf, 0, b, 0, buf.Length)
-
Return buf.Length
-
Case 1
-
buf = Encoding.ASCII.GetBytes("BBBBBBBBBB")
-
Buffer.BlockCopy(buf, 0, b, 0, buf.Length)
-
Return buf.Length
-
Case 2
-
buf = Encoding.ASCII.GetBytes("CCCCCCCCCC")
-
Buffer.BlockCopy(buf, 0, b, 0, buf.Length)
-
Return buf.Length
-
Case 3
-
buf = Encoding.ASCII.GetBytes("DDDDDDDDDD")
-
Buffer.BlockCopy(buf, 0, b, 0, buf.Length)
-
Return buf.Length
-
Case 4
-
buf = Encoding.ASCII.GetBytes("EEEEEEEEEE")
-
Buffer.BlockCopy(buf, 0, b, 0, buf.Length)
-
Return buf.Length
-
Case 5
-
buf = Encoding.ASCII.GetBytes("FFFFF")
-
Buffer.BlockCopy(buf, 0, b, 0, buf.Length)
-
Return buf.Length
-
Case Else
-
Return 0
-
End Select
-
End Function
-
-
End Class
0 1895 Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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
|
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
|
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
|
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...
| |
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
|
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
|
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;
|
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...
|
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...
|
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,...
| |
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...
|
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...
|
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();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |