CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2003
    Posts
    3

    Problems with queue in system.collections

    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
    Join Date
    Dec 2000
    Location
    Slovakia
    Posts
    1,043
    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 loop
    Code:
    for (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

  3. #3
    Join Date
    Nov 2002
    Location
    Singapore
    Posts
    1,890
    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

    Paresh

  4. #4
    Join Date
    Nov 2002
    Location
    Singapore
    Posts
    1,890
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured