Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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 ?

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

Posted

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.

string input, output;
input = "test string";
		
Queue myQueue = new Queue();
myQueue.Enqueue(input);

output = (string)myQueue.Dequeue();
MessageBox.Show(output);

Anybody looking for a graduate programmer (Midlands, England)?
Posted

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 !

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

object packet = cmdQueue.Dequeue(); 
byte n1 = ((byte[])packet)[0];

or alternatively store the object returned by Dequeue directly into the correct object type.

byte[] packet = cmdQueue.Dequeue();
byte n1 = packet[0];

Anybody looking for a graduate programmer (Midlands, England)?
Posted

The second command didn't work because I forgot the cast. It should have read read...

byte[] packet = (byte[])cmdQueue.Dequeue();
byte n1 = packet[0];

Anybody looking for a graduate programmer (Midlands, England)?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...