private void button1_Click(object sender, EventArgs e)
            Decimal[] mydecimal = new Decimal[10];

            for (int i = 0; i < mydecimal.Length; i++)
                mydecimal[i] += i;


            foreach (Decimal mydec in mydecimal)
                int count = 0;

                MessageBox.Show("I am button "+ Convert.ToInt32(count)+" in mydecimal");
                count += count + 1;


I have this code and have made an array of decimal objects....

Each time i run it... it comes as "I am button 0 in mydecimal".

I want it to say..."I am button 1 in mydecimal", "I am button 2 in mydecimal" etc


Apologies..silly mistake..sorted it now...I just declared the count outside of the foreach loop...

could anyone explain why this is though please?

I think it may be because you cannot assign values to a variable within a foreach loop?

count is reinitialized to 0 with every iteration of the loop, so any changes you make to it will be lost on the next iteration.

This question has already been answered. Start a new discussion instead.