0

Hi

IM trying to make a queue of Byte[] every time i click on a button , Very Strait Foreword

System.Collections.Queue Myqueue= new System.Collections.Queue(20);
public byte[] Bytearray = new byte[2];

Myqueue.Clear();

while (Myqueue .Count  !=20)
    {

        Temp=Calc(Bytearray);

        Bytearray[0] = temp[8];
        Bytearray[1] = temp[13];
        Myqueue.Enqueue(Bytearray);  

     }

So the Loop is done 20 times, BUT all the Data in the queue are the same!

so the first time Bytearray is [9][66] , then its [3][21] , then all the Queue is [3][21] and when Bytearray is [0][5] all the queue is [0][5] and so on !

Of course temp is changing and i checked that in the debugging and by printing its values on the screen

As u can see in the Attached Image

[IMG]http://i670.photobucket.com/albums/vv69/fx2236/Capture.jpg[/IMG]

Am i doing something wrong here or is it a bug or just Bad programming Skills which i know i have!

2
Contributors
5
Replies
6
Views
7 Years
Discussion Span
Last Post by sknake
0

The array is by reference and you're re-using the same reference over and over:

System.Collections.Queue Myqueue= new System.Collections.Queue(20);
public byte[] Bytearray = new byte[2];

Myqueue.Clear();

while (Myqueue .Count  !=20)
    {
        Bytearray = new byte[2]; //Instantiate a new array
        Temp=Calc(Bytearray);

        Bytearray[0] = temp[8];
        Bytearray[1] = temp[13];
        Myqueue.Enqueue(Bytearray);  

     }
0

i figured that out , but as im still new to C# , how do i solve this problem ?

0

I gave you the solution:

System.Collections.Queue Myqueue= new System.Collections.Queue(20);
public byte[] Bytearray = new byte[2];

Myqueue.Clear();

while (Myqueue .Count  !=20)
    {
        Bytearray = new byte[2]; //Instantiate a new array
        Temp=Calc(Bytearray);
        Bytearray[0] = temp[8];
        Bytearray[1] = temp[13];
        Myqueue.Enqueue(Bytearray);  

     }

Edited by sknake: n/a

0

I gave you the solution:

System.Collections.Queue Myqueue= new System.Collections.Queue(20);
public byte[] Bytearray = new byte[2];

Myqueue.Clear();

while (Myqueue .Count  !=20)
    {
        Bytearray = new byte[2]; //Instantiate a new array
        Temp=Calc(Bytearray);
        Bytearray[0] = temp[8];
        Bytearray[1] = temp[13];
        Myqueue.Enqueue(Bytearray);  

     }

i tried to do that early but it didn't solve the problem
[IMG]http://i670.photobucket.com/albums/vv69/fx2236/Capture-1.jpg[/IMG]

the first Value is different because i en-queued it before the loop

0

Well your loop probably executes in less than 1second so if you're reading temperatures from an external sensor it is possible you're asking 20 times back-to-back-to-back and get the same value. Upload a sample project or post the code for all classes involved. You haven't provided enough information to diagnose the problem.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.