need an iterable collection which repeats or cycles its values

  • Thread starter Thread starter metaperl
  • Start date Start date
M

metaperl

Hi, I wrote a Forms program which toggles a label each time you click
it:
private void button1_Click(object sender, EventArgs e)
{
label1.Text = (label1.Text == "hello") ? "goodbye" :
"hello" ;
}

but in the interest of scaleability, I want this program to store the
label values in a collection, access them via Next() and cycle back to
the beginning when the end of the collection is reached.

Any pointers to something in the .NET library which allows this?
 
metaperl said:
Hi, I wrote a Forms program which toggles a label each time you click
it:
private void button1_Click(object sender, EventArgs e)
{
label1.Text = (label1.Text == "hello") ? "goodbye" :
"hello" ;
}

but in the interest of scaleability, I want this program to store the

... very enterprisey ;)
label values in a collection, access them via Next() and cycle back to
the beginning when the end of the collection is reached.

Any pointers to something in the .NET library which allows this?

I don't think there is such a collection in the .NET library. You'll
probably have to create your own custom collection.

Besides only adding the label values you could also add delegates to the
collection. This way you could create a method Execute() which executes
the Method associated with the current label.

Max
 
Well there certainly is a datastructure that can do just this. A queue


Queue<string> foo = new Queue<string>();
foo.Enqueue("Hello");
foo.Enqueue("Hello2");
foo.Enqueue("Hello3");


then whenever you need a string you simply use

string s = foo.Dequeue();
foo.Enqueue(s);


This will provide your wrap around functionality for you.

Cheers,

Greg Young
MVP - C#
http://codebetter.com/blogs/gregyoung
 
Greg said:
Well there certainly is a datastructure that can do just this. A queue


Queue<string> foo = new Queue<string>();
foo.Enqueue("Hello");
foo.Enqueue("Hello2");
foo.Enqueue("Hello3");


then whenever you need a string you simply use

string s = foo.Dequeue();
foo.Enqueue(s);


This will provide your wrap around functionality for you.

If we were being polite, we would of course wrap this 'bizarro queue' in
a class called CyclicList<T>, wouldn't we... :)
 
haha .. yes that we would .. ah the life of a maintenance programmer.

Although I do have a certain fondness for BizarroQueue .. perhaps I would
make it add to the head occasionally too to make it truely bizarre.
 
Back
Top