|
-
Problems with system.collections Queue class
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();
}
}
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks