Click to See Complete Forum and Search --> : Problems with queue in system.collections


etogoe
January 15th, 2003, 05:53 AM
Hi,

I am trying to use the Queue class from .NET System.Collections using C#
but I am getting behaviour I can not explain. I suspect there is a fundamental
issue I am missing but I am unable to find the solution.

The task is simple:

I want to create queue handling objects of my class

The problem is that when I Dequeue from the Queue
all objects dequeued are equal to the last queued object

When inspecting the content of the queue using foreach
all objects in the queue also seems to be equal

Can somebody point me in the right direction of an answer ?

CODE PRINTOUT:

What goes in should also come out:
Info<Event info 0>
Info<Event info 1>
Info<Event info 2>
Info<Event info 3>
Info<Event info 4>
Inspecting the content of the queue shows the problem:
Info<Event info 4>
Info<Event info 4>
Info<Event info 4>
Info<Event info 4>
Info<Event info 4>
And this is what comes out when dequeing:
Info<Event info 4>
Info<Event info 4>
Info<Event info 4>
Info<Event info 4>
Info<Event info 4>

CODE PRODUCING PRINTOUT:

using System;
using System.Collections;

namespace TestOfCollections
{
public class Event
{
string info;

public void Print()
{
Console.WriteLine("Info" + info);
}
public void Put(string str)
{
info = str;
}
public string Get()
{
return info;
}
}

public class TestClass
{
public void Run()
{
int i;

Event evnt = new Event();
Queue myQ = new Queue();

Console.WriteLine("What goes in should also come out:");

for (i = 0;i < 5;i++)
{
evnt.Put("<Event info "+i+">");
evnt.Print();
myQ.Enqueue(evnt);
}

Console.WriteLine("Inspecting the content of the queue shows the problem:");

foreach (Event e in myQ)
{
e.Print();
}

Console.WriteLine("And this is what comes out when dequeing:");

while (myQ.Count > 0)
{
evnt = (Event) myQ.Dequeue();
evnt.Print();
}
}
}
}

MartinL
January 15th, 2003, 08:51 AM
Yes, you are right. It is fundamental issue...

You have created one Event object and you have added it into the queue five times. The same object.

In the loopfor (i = 0;i < 5;i++)
{
evnt.Put("<Event info "+i+">");
evnt.Print();
myQ.Enqueue(evnt);
}you just call the Put method five times (on the same object).

So, the behaviour you described in your post is correct...

martin

pareshgh
January 18th, 2003, 03:26 AM
what exactly you are doing is when you add the same object event the reference to that object becomes same to all object.

for example ,
(as martin indicated - you added 1 object 5 times)
thus refering to your code

for (i = 0;i < 5;i++)
{
evnt.Put("<Event info "+i+">");
evnt.Print();
myQ.Enqueue(evnt);
}

what happens here is evnt object is added 5 times. suppose you add string 5 times then it won't be a problem coz 5 objects of stirng are added. but evnt object remains same and it holds the last value before comming out of the loop which is i=4.
so you are getting the result as you described. however i modified a code a bit and will give you the result as you expect to be,

***************************************
using System;
using System.Collections;

namespace TestOfCollections
{
public class Event
{
string info;

public void Print()
{
Console.WriteLine("Info" + info);
}
public void Put(string str)
{
info = str;
}
public string Get()
{
return info;
}
}

public class TestClass
{
public static void Main(string [] arg)
{
int i;

Queue myQ = new Queue();

Console.WriteLine("What goes in should also come out:");

for (i = 0;i < 5;i++)
{
Event evnt = new Event();
evnt.Put("<Event info "+i+">");
evnt.Print();
myQ.Enqueue(evnt);
}


Console.WriteLine("Inspecting the content of the queue shows the problem:");

foreach (Event e in myQ)
{
e.Print();
}

Console.WriteLine("And this is what comes out when dequeing:");

while (myQ.Count > 0)
{
Event e1 = (Event) myQ.Dequeue();
e1.Print();
}
}
}
}

***************************************

and the result of the above code after running will be,
What goes in should also come out:
Info<Event info 0>
Info<Event info 1>
Info<Event info 2>
Info<Event info 3>
Info<Event info 4>
Inspecting the content of the queue shows the problem:
Info<Event info 0>
Info<Event info 1>
Info<Event info 2>
Info<Event info 3>
Info<Event info 4>
And this is what comes out when dequeing:
Info<Event info 0>
Info<Event info 1>
Info<Event info 2>
Info<Event info 3>
Info<Event info 4>


----------------
Is that what you want as desired result. since new objects new values :D :cool: :)

Paresh

pareshgh
January 18th, 2003, 03:28 AM
I forgot to mention in above post
that here was the code change,

for (i = 0;i < 5;i++)
{
Event evnt = new Event(); // inside instead of outside
evnt.Put("<Event info "+i+">");
evnt.Print();
myQ.Enqueue(evnt);
}

in above skeleton of your code I kept evnt object inside

and bellow I changed

while (myQ.Count > 0)
{
Event e1 = (Event) myQ.Dequeue();
e1.Print();
}

the evnt as e1

Paresh