473,406 Members | 2,549 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

byte array via a class property

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.
Nov 21 '05 #1
12 2655
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?
Nov 21 '05 #2
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?
Nov 21 '05 #3

"Saurabh Kumar" <au@sd.com> wrote
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
Nov 21 '05 #4

"Larry Serflaten" <se*******@usinternet.com> wrote in message
news:et**************@TK2MSFTNGP09.phx.gbl...

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.

Nov 21 '05 #5

"Saurabh Kumar" <au@sd.com> wrote
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.....
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
Nov 21 '05 #6

"Larry Serflaten" <se*******@usinternet.com> wrote in message
news:u1**************@TK2MSFTNGP12.phx.gbl...

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.
Nov 21 '05 #7
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!!
Nov 21 '05 #8

"Saurabh Kumar" <au@sd.com> wrote
There are good reasons why you should post a small demo
to reproduce the problem.
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


Nov 21 '05 #9
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
Nov 21 '05 #10

"Saurabh Kumar" <au@sd.com> wrote
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


Nov 21 '05 #11
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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 21 '05 #12
Hats off to you!, the redim did the magic.
Thanks.
Nov 21 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Ray Z | last post by:
So far, I get the idea that if I want to use both the unmanaged and managed memory, I can not avoid memory copy. But I DO need to avoid it. I get a idea that maybe I could use "union" to convert...
0
by: Olav Langeland | last post by:
I have a C# Form with a System.Windows.Forms.PropertyGrid(). This uses a Datagram class, implemented in both C# and C++: // C# public class Datagram { public byte _Buffer; // some code...
5
by: Naveen Mukkelli | last post by:
Hi, I've got a property that returns byte. For example, private byte bytes; public byte ReturnsByteArray { get
1
by: Taiwo | last post by:
I generated a Typed Dataset class including a base64Binary column. This column was specified as a .NET type of Byte() in the class that was auto-generated. I set the value of this property to New...
6
by: Denis C | last post by:
Hi I have a byte array of length 2404. I want to access subsets of the array and pass the subset to the constructor of a class. i.e. Dim bData(2403) As Byte Dim aThing As SomeClass = new...
6
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)...
3
by: simonc | last post by:
Can you define a property as type Byte array of a specific length? I am trying to pass a byte array which is 3200 bytes in length from one form (in which the bytes are read from a file) to...
17
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
10
by: Scott Townsend | last post by:
So I need to talk to a devices that expects all of the bits and bytes I sent it to be in specific places (not yet 100% defined). I wanted to create a structure/class with all of the data in it...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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,...
0
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...
0
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,...

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.