Hi.....

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

thanks

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.

Doh...that was really stupid of me..thanks for reply

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.