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!

Recommended Answers

All 5 Replies

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);  

     }

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

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 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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.