Jedhi Posted January 18, 2006 Posted January 18, 2006 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 ? Quote
Administrators PlausiblyDamp Posted January 18, 2006 Administrators Posted January 18, 2006 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... Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Jedhi Posted January 18, 2006 Author Posted January 18, 2006 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. Quote
Cags Posted January 18, 2006 Posted January 18, 2006 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); Quote Anybody looking for a graduate programmer (Midlands, England)?
Jedhi Posted January 18, 2006 Author Posted January 18, 2006 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 ! Quote
Cags Posted January 18, 2006 Posted January 18, 2006 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]; Quote Anybody looking for a graduate programmer (Midlands, England)?
Jedhi Posted January 18, 2006 Author Posted January 18, 2006 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 ? Quote
IngisKahn Posted January 19, 2006 Posted January 19, 2006 Or better yet you can treat C# like an object oriented language and not use an object array. :p Quote "Who is John Galt?"
Cags Posted January 19, 2006 Posted January 19, 2006 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]; Quote Anybody looking for a graduate programmer (Midlands, England)?
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.