PC Review


Reply
Thread Tools Rate Thread

byte array via a class property

 
 
Saurabh Kumar
Guest
Posts: n/a
 
      25th Nov 2004
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.


 
Reply With Quote
 
 
 
 
Saurabh Kumar
Guest
Posts: n/a
 
      25th Nov 2004
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?


 
Reply With Quote
 
Saurabh Kumar
Guest
Posts: n/a
 
      25th Nov 2004
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?


 
Reply With Quote
 
Larry Serflaten
Guest
Posts: n/a
 
      25th Nov 2004

"Saurabh Kumar" <(E-Mail Removed)> 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
 
Reply With Quote
 
Saurabh Kumar
Guest
Posts: n/a
 
      26th Nov 2004

"Larry Serflaten" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> 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.



 
Reply With Quote
 
Larry Serflaten
Guest
Posts: n/a
 
      26th Nov 2004

"Saurabh Kumar" <(E-Mail Removed)> 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
 
Reply With Quote
 
Saurabh Kumar
Guest
Posts: n/a
 
      26th Nov 2004

"Larry Serflaten" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> 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.


 
Reply With Quote
 
Saurabh Kumar
Guest
Posts: n/a
 
      26th Nov 2004
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!!


 
Reply With Quote
 
Larry Serflaten
Guest
Posts: n/a
 
      26th Nov 2004

"Saurabh Kumar" <(E-Mail Removed)> 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








 
Reply With Quote
 
Saurabh Kumar
Guest
Posts: n/a
 
      26th Nov 2004
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


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to convert a class/struct to a byte array? chrisben Microsoft C# .NET 0 7th Oct 2008 07:30 PM
Serializing a class into a byte array? Ole Microsoft Dot NET Compact Framework 6 29th Sep 2008 08:53 AM
Structure/Class to Byte Array and Back again Scott Townsend Microsoft VB .NET 8 9th Jun 2008 01:13 AM
assign byte array to struct or class Christian Havel Microsoft C# .NET 2 10th Mar 2008 11:14 AM
Class property as a Byte array =?Utf-8?B?c2ltb25j?= Microsoft VB .NET 3 21st Oct 2004 09:33 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:46 PM.