Error in color changer function

I have two colors "red" also "salmon". I need to create a dynamiclly panel also the background color of the panel. These colors should be between two colors (red

 public Color x, y;
        protected void Page_Load(object sender, EventArgs e)
        {
            BackGroundColorArranger(Color.Red, Color.Salmon);
        }
        void BackGroundColorArranger(Color x, Color y)
        {

            BackGroundColorArrangerBase(Convert.ToInt32((float)(x.ToArgb() + y.ToArgb()) / 2));
        }
        void BackGroundColorArrangerBase(int z)
        {
            Panel panel = new Panel();
            panel.ID = z.ToString();
            panel.Width = 150;
            panel.Height = 50;
            panel.BackColor = Color.FromArgb(z);
            this.Controls.Add(panel);
            BackGroundColorArranger(x, Color.FromArgb(z));
        }

But how can I do this. Above codes give me stackoverflow error.

+3
source share
5 answers

As they said, you have an infinite recursive loop, and why you get stack overflows.

As a quick fix, remove this line from BackGroundColorArrangerBase:

BackGroundColorArranger(x, Color.FromArgb(z));

So it looks like this:

        void BackGroundColorArrangerBase(int z) 
        {
           Panel panel = new Panel();
           panel.ID = z.ToString(); //this wil generate the same id for the same pair of colors
           panel.Width = 150;
           panel.Height = 50;
           panel.BackColor = Color.FromArgb(z);
           this.Controls.Add(panel);
        }

. , , . , , BackGroundColorArranger -WITH A DIFFERENT COLOR PAIR, colorpair .

, , BackGroundColorArranger ... , . ArrangerBase, .

0

BackGroundColorArranger . , , "" β†’ Windows β†’ " ", .

+4

#, , .

?

+2

, ( .. , ):

:

    void BackGroundColorArranger(Color, Color)
    {
        BackGroundColorArrangerBase(int);
    }
    void BackGroundColorArrangerBase(int)
    {
        BackGroundColorArranger(Color, Color);
    }
0

, , .

, , , .

0

Source: https://habr.com/ru/post/1710319/


All Articles