views:

128

answers:

2

My below Codes gives me error:"Index was outside the bounds of the array." My Algorithms create Colorset arrays that's arrays dimention '16', But i need Second one 'colorSetLegend' that's dimensions:32 if you look below Bold codes that returns me error.

 Color[] colorSetLegend = new Color[32];
            Color[] colorSet = { Color.Red, Color.Blue, Color.Green, Color.Yellow };
            Color end = Color.White;
            colorSet = ColorMaker.GenerateColor(colorSet, end);

            for (int i = 0; i < colorSet.Length; )
            {
                for (int j = 0; j < colorSetLegend.Length; )
                {
                    colorSetLegend[j] = colorSet[i];
                    colorSetLegend[j++] = Color.Black;
                    i++;
                }
            }

My Color generator below:


public class ColorMaker
{
    public static Color[] GenerateColor(Color[] baseColorSet, Color end)
    {
        Color[] colorSet = new Color[16];
        int j = 0;
        foreach (Color start in baseColorSet)
        {
            for (int i = 0; i < 15; i += 4)
            {
                int r = Interpolate(start.R, end.R, 15, i),
                    g = Interpolate(start.G, end.G, 15, i),
                    b = Interpolate(start.B, end.B, 15, i);

                colorSet[j] = Color.FromArgb(r, g, b);
                j++;
            }
        }

        return colorSet;

    }
    static int Interpolate(int start, int end, int steps, int count)
    {
        float s = start, e = end, final = s + (((e - s) / steps) * count);
        return (int)final;
    }
}
+2  A: 

You're incrementing i in your inner loop. I suspect you meant to do it in your outer loop - otherwise during one iteration of your outer loop, you're incrementing i many times, until you exceed the bounds of the array.

Alternatively, you could write your for loops the same way everyone else does:

for (int i = 0; i < colorSet.Length; i++)
{
    for (int j = 0; j < colorSetLegend.Length; j++)
    {
        colorSetLegend[j] = colorSet[i];
        colorSetLegend[j] = Color.Black;
    }
}

Having said that, the code's a bit pointless given that the first line inside the loop sets colorSetLegend[j] and the second line sets the same element again. Furthermore, on the next iteration of the outer loop you'll be overwriting all the values in colorSetLegend all over again. What are you trying to accomplish?

Marc made a good-looking guess at your aim here (although he's now deleted his answer!)

Here's his guess at working code for what you want:

for (int i = 0; i < colorSet.Length; i++)
{
    colorSetLegend[i*2] = colorSet[i];
    colorSetLegend[(i*2)+1] = Color.Black;
}

A few things to learn from this, if he's right:

  • Think about the level of nesting of your loops. Did you really mean to have two loops here?
  • Try to use conventional idioms for looping - whenever I see an empty bit at the end of the start of a for loop, I get nervous
  • Using pre- and post-increment operators in another expression is easy to get wrong.
Jon Skeet
ForExample;colorSetLegend[0] = "Red";colorSetLegend[1] = Color.Black;colorSetLegend[2] = "FireBrick";colorSetLegend[3] = Color.Black;
Phsika
Sounds like Marc's guess was right then. Have a look at my edited answer.
Jon Skeet
A: 

This will achieve what you are looking for:

int j = 0;
for (int i = 0; i < colorSet.Length; i++)
{
    colorSetLegend[j++] = colorSet[i];
    colorSetLegend[j++] = Color.Black;
}
Lazarus
That will work, yes - but I prefer Marc's answer (now copied into mine) as it doesn't use pre/post-increment within a statement. That always makes me stop and check that it really is doing what I need it to - and anything that harms readability should be avoided where possible, IMO.
Jon Skeet