need an iterable collection which repeats or cycles its values


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?
 
Ad

Advertisements

M

Markus Stoeger

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
 
G

Greg Young

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
 
L

Larry Lard

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... :)
 
Ad

Advertisements

G

Greg Young

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top