byte array via a class property

S

Saurabh Kumar

I have a byte array, which i need to send to a class, where it will be
stored in an arraylist.

I use the following property in the class to accept the byte array, but it
seems that the array is getting corrupted somehow. Is my declaration wrong
somewhere?

Public WriteOnly Property BytesAdd() As Byte()
Set(ByVal Value() As Byte)
pBytes.Add(Value)
End Set
End Property



Thanks.
 
S

Saurabh Kumar

ok, i did some debugging and found that though pBytes is an arraylist, every
time the property does the .add, a new entry is added to the arraylist as
expected, BUT all the previous entries in the arraylist become equal to this
entry!! Any reason why?
 
S

Saurabh Kumar

Is it that perhaps every time i add a new entry of the byte array, the entry
is made as a reference and not a a value, which causes all previous entries
to reflect the latest addition?
 
L

Larry Serflaten

Saurabh Kumar said:
I have a byte array, which i need to send to a class, where it will be
stored in an arraylist.

I use the following property in the class to accept the byte array, but it
seems that the array is getting corrupted somehow. Is my declaration wrong
somewhere?

Public WriteOnly Property BytesAdd() As Byte()
Set(ByVal Value() As Byte)
pBytes.Add(Value)
End Set
End Property


You haven't shown your declaration for pBytes, or what you are passing
to the property. Was it corrupted before it got passed? No one can tell
from just the property code.

Create a small demo to illustrate the problem, that can be posted into a
project for others to inspect and test. It is far easier to fix your code than
guess what you may be using.....

LFS
 
S

Saurabh Kumar

Larry Serflaten said:
You haven't shown your declaration for pBytes, or what you are passing
to the property. Was it corrupted before it got passed? No one can tell
from just the property code.

Create a small demo to illustrate the problem, that can be posted into a
project for others to inspect and test. It is far easier to fix your code
than
guess what you may be using.....

LFS

Here is the probem in more details, and my inspection detials till now:

class A has a variable
Private byteBuf(512) As Byte

this variable gets new data every now and then.

Whenever it gets the data it sends this byte array to another class B via a
property by the synatx:

sw.BytesAdd = byteBuf

Where sw is the instance of class B, declared in a module.

Inside class B, the BytesAdd property is defined as:

Public WriteOnly Property BytesAdd() As Byte()
Set(ByVal Value() As Byte)
pBytes.Add(Value)
End Set
End Property

pbytes is defined in class B as:
Dim pBytes As New ArrayList



On checking i found that the values coming inside the property are corect.
The 'value' variable seems to be getting the correct value, and the first
entry made in the pByte arraylist is also correct, but from the next
entries, all previous entries of the arraylist reflec the last entry data.

Any suggestions to solve this problem would be great.

Thanks.
 
L

Larry Serflaten

On checking i found that the values coming inside the property are corect.
The 'value' variable seems to be getting the correct value, and the first
entry made in the pByte arraylist is also correct, but from the next
entries, all previous entries of the arraylist reflec the last entry data.

Any suggestions to solve this problem would be great.


How about the code you are using to check the values only references
the last entry? Again, without seeing exactly what you are using, no
one can know. There are good reasons why you should post a small demo
to reproduce the problem. Including the statement above, it may happen
that you actually find the error when you try to build the demo. If not,
you can post that code so others can inspect exactly what you are using....

LFS
 
S

Saurabh Kumar

Larry Serflaten said:
How about the code you are using to check the values only references
the last entry? Again, without seeing exactly what you are using, no
one can know. There are good reasons why you should post a small demo
to reproduce the problem. Including the statement above, it may happen
that you actually find the error when you try to build the demo. If not,
you can post that code so others can inspect exactly what you are
using....

LFS

I am not using an code to check the values of the arraylist, but a
breakpoint and the locals window.
Regarding posting the complete code or the project, is not possible, because
this is part of a bigger project, and i cant possibly post it here. What i
have mentioned i my previous posts almost completely reflects the code
related to the problem at hand.
 
S

Saurabh Kumar

ok continuing my reasearch i made another discovery!

See, the byte data i am getting from a socket connection.

So, as soon as my class A does:
byteLen = cSocket.Receive(byteBuf, byteBuf.Length, 0)

All the values of pByte in class B get replaced to this new values of
byteBuf in class A!!
 
L

Larry Serflaten

Regarding posting the complete code or the project, is not possible,

Do not post your entire project, create a new project, and reproduce
the problem in a _SMALL_ demo in that new project. You can post
text explainations all year long, but it will not show exactly what you
are doing that causes the problem.

Create a SMALL demo in a new project that can be pasted into a
form or class to see the problem. You can work on it there, and
may even find a solution yourself, but if not, you'd have that demo
to post to the group.

As I said earlier, it is far easier for others to fix your code, than to
guess at what you may be doing that causes the error....

LFS
 
S

Saurabh Kumar

Ok Larry you win :)

I have started a new thread with the class files attached as is for your
reference. The thread is called:

ByVal also seems to pass array ByRef
 
L

Larry Serflaten

Saurabh Kumar said:
Ok Larry you win :)

I'd guess not. A SMALL demo means just what is needed to reproduce the
problem. That would have meant isolating the arraylist, the property and
adding a means to pass in an array. If you would have added that to a new
form, you could have passed the property an array from the click of a button,
and tested just that part of it, without all that other FTP code. Something like
that can be copy and pasted into a new form and ran. What you posted would
not run, so it did not reproduce the problem.

Isolating the problems to just their bare essentials will help you solve the
problems yourself! If you get stuck, only the problem code need be
posted, not a whole project, or class.

I have started a new thread with the class files attached as is for your
reference. The thread is called:

ByVal also seems to pass array ByRef

ByVal does not mean you get a copy of the array, it means you get a
copy of the pointer to the array. Both are references to the same array
and therein lies the problem. When you pass the same array into the
property, another reference to that same array is added to the arraylist.
No matter how many times you clear it and reload it with data, it is still
the same array sitting on the managed heap. What you want is a new
array instead of just clearing out the old data and filling it up with new
data.

Instead of:

m_aBuffer.Clear(m_aBuffer, 0, m_aBuffer.Length)

Try this:

ReDim m_aBuffer(BLOCK_SIZE)


LFS
 
M

Mattias Sjögren

passes the byte array to the class1.vb via property, but every new entry
added to the class1 arraylist, causes ALL previous entries of the arraylist
to reflect the latest array data...as if all were pointing to the same
reference variable.

Arrays are reference types, so what you're passing around is a
reference to the same array instance.

The array reference is passed by value, not the array data. So if
you'd re-assign Value in the BytesAdd property set procedure, it
wouldn't affect the caller.



Mattias
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top