Queue

Jedhi

Centurion
Joined
Oct 2, 2003
Messages
127
public Queue exQueue = new Queue();

cmdQueue.Enqueue(new object[3]{byte n1, byte[] data});

Later .....

object packet = cmdQueue.Dequeue();

Now I want to extract the n1 and data from the packet, but It does not seems that I have any luck with that. Any suggestions ?
 
What language is that written in> I guessed at C# but as it doesn't compile I've no idea what you are trying to do or what the problem you are experiencing might be...
 
PlausiblyDamp said:
What language is that written in> I guessed at C# but as it doesn't compile I've no idea what you are trying to do or what the problem you are experiencing might be...

It is in C#. I am trying to queue some data and extract some data and remove it from the Queue when some circumstances are meet.
 
Without more detail it's going to be very hard to help. Theres nothing inherantly wrong with the code you have posted (aside from you declaring a Queue as exQueue, and then calling methods of cmdQueue).

I tested the code below and it works fine.
C#:
string input, output;
input = "test string";
			
Queue myQueue = new Queue();
myQueue.Enqueue(input);

output = (string)myQueue.Dequeue();
MessageBox.Show(output);
 
Well the problem is here

object packet = cmdQueue.Dequeue();

byte n1 = packet[0] // Does not work

byte[] data =

I want to get n1 and data that is inside the packet. This is my problem !
 
Jedhi said:
Well the problem is here

object packet = cmdQueue.Dequeue();

byte n1 = packet[0] // Does not work

byte[] data =

I want to get n1 and data that is inside the packet. This is my problem !

The reason that doesn't work is because you are trying to reference an index of packet, but packet is an object not an array. While the object type will allow you to store an array you cannot access it correctly without casting it back to its original type. I believe this process is called boxing and unboxing. Hence in your code you are boxing the array into an object type, in order to use it as an array again you will have to unbox it. (Somebody more in-tune with programming terms might confirm this or explain it better).

You need to do either this...
C#:
object packet = cmdQueue.Dequeue(); 
byte n1 = ((byte[])packet)[0];
or alternatively store the object returned by Dequeue directly into the correct object type.
C#:
byte[] packet = cmdQueue.Dequeue();
byte n1 = packet[0];
 
You got it, but only your first suggestion will work. Next How do you take the data that is a byte array out of the object ?
 
Back
Top