2

How can I enqueue value of a queue (not it reference) to another queue? It work like I have a queue of points in C++ (Queue<*word>), but I want to copy value of buffer queue like this

a = 1;
int[] array = new int[1]
array[0] = a //array[0] now is 1
a = 0 // but array[0] doesn't change, array[0] is 1!

I have problem in words.Enqueue(buffer)

using word = System.Collections.Generic.Queue<char>;

Queue<word> words = new Queue<word>();  //word is the custom type, that was def in file top
word buffer = new word();

for (var symbol_count = 0; symbol_count < text.Length; ++symbol_count)
{

    if (text[symbol_count] != ' ' && text[symbol_count] != '.' && text[symbol_count] != ',')
    {
        buffer.Enqueue(text[symbol_count]); //store one char in word
    } 
    else 
    {
        buffer.Enqueue(text[symbol_count]); //store end of word symbol
        words.Enqueue(buffer);  //store one word in words queue, but compiler do it like I try to copy a reference of buffer, not it value!!!
        //System.Console.WriteLine(words.Count); DEBUG
        buffer.Clear(); //clear buffer and when i do this, value in words queue is deleted too!!!
    }
}
Sach
  • 10,091
  • 8
  • 47
  • 84
  • *"I have problem in words.Enqueue(buffer)"*. So what is the problem? – Sach Aug 01 '19 at 19:24
  • I insert in "words" queue not a value of "buffer" queue, but it's pointer. And when I change a value of "buffer" queue, it also change in "words" queue. But I want just copy a value of "buffer" queue in "words" queue – Magician Artemka Aug 01 '19 at 19:26
  • 1
    It's a little confusing to read when you alias `Queue` – Rufus L Aug 01 '19 at 19:31
  • 2
    Duplicate of https://stackoverflow.com/questions/16209747/cloning-queue-in-c-sharp. Or just create a new queue instead of `Clear`ing the old one. – Eugene Podskal Aug 01 '19 at 19:34
  • @Rufus L, sorry for this, I thought that this trick improve readability of code :C – Magician Artemka Aug 01 '19 at 19:34
  • 1
    I'm not sure if I understand your question fully, but it sounds like you want to make a deep copy of a value in your queue and enqueue it in a different queue? Here's a link that might help: https://stackoverflow.com/questions/129389/how-do-you-do-a-deep-copy-of-an-object-in-net-c-specifically – dustinos3 Aug 01 '19 at 19:35
  • Yes, "deep copy" that's what I need. Thank you! – Magician Artemka Aug 01 '19 at 19:36

2 Answers2

3

The problem is that you're re-using the same buffer in your loop, so when you clear it, all the references to it are also cleared.

Instead, set the local buffer variable to a new instance of the object, so that any changes to it don't affect the reference we just stored:

foreach (char chr in text)
{
    buffer.Enqueue(chr);

    if (chr == ' ' || chr == '.' || chr == ',')
    {                     
        words.Enqueue(buffer);

        // reassign our local variable so we don't affect the others
        buffer = new Queue<char>();   
    }
}
Rufus L
  • 36,127
  • 5
  • 30
  • 43
1

When you save a new word to a queue (you might have a bug with the last word here, by the way)

words.Enqueue(buffer);

You shouldn't use the buffer variable itself: it holds a reference to the temporary data and you need to make a copy of it first, something that will not be modified in the next lines.

Try e.g.

words.Enqueue(new word(buffer));
DK.
  • 3,173
  • 24
  • 33
  • Thank you for help! – Magician Artemka Aug 01 '19 at 19:45
  • @MagicianArtemka you are welcome. Note though, that I was answering your question here, basically _how to use queue value and "detach" it from queue reference_. For the underlying problem, however, the code by Rufus is more straightforward and efficient. – DK. Aug 01 '19 at 20:23