Problems with system.collections Queue class


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Problems with system.collections Queue class

  1. #1
    Geir Guest

    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();
    }
    }
    }
    }


  2. #2
    Boris Karadjov Guest

    Re: Problems with system.collections Queue class

    You are creating a single instance of the Event object and then you are
    enqueuing this instance several times.

    Calling evnt.Put changes the internal state of the object, but Queue does
    not record the state (using serialization, for example), it simply stores a
    reference to the object.

    Same object = same reference = identical items in the queue.
    --
    Boris Karadjov
    Brainbench MVP for Visual C++
    http://www.brainbench.com


    "Geir" <geir.ovsttun@ericsson.no> wrote in message
    news:3e254638$1@tnews.web.devx.com...
    >
    > 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();
    > }
    > }
    > }
    > }
    >




  3. #3
    NewKid Guest

    Re: Problems with system.collections Queue class


    "Geir" <geir.ovsttun@ericsson.no> wrote:
    >[cut]
    >The problem is that when I Dequeue from the Queue
    >all objects dequeued are equal to the last queued object


    That's because you are placing in the queue the same object over and again.
    You are just changing the event text, but within the same object. Then, since
    all items in the queue are the same object, they all have the same text (with
    number 4, after finishing 'for' loop).
    The solution is to create new Event object for every item to place in queue.

    >CODE PRODUCING PRINTOUT:
    >[cut]
    >using System;
    >using System.Collections;
    >
    >namespace TestOfCollections
    >{
    > public class Event
    > {
    > string info;
    > public void Print() [...]


    // I would recommend Info property here
    public string Info
    {
    get {return info;}
    set {info = value;}
    }
    > }
    >
    > public class TestClass
    > {
    > public void Run()
    > {
    > int i;

    // just a placeholder
    > Event evnt = null;
    > Queue myQ = new Queue();
    >
    > Console.WriteLine("What goes in should also come out:");
    >
    > for (i = 0;i < 5;i++)
    > {

    // new Event here
    evnt = new Event();
    evnt.Info = "<Event info "+i+">";
    > evnt.Print();

    // new object gets enqueued
    > myQ.Enqueue(evnt);
    > }

    [...]
    > }
    > }
    >}
    >



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center