Stack Overflow error on Color Changer Function

試著忘記壹切 提交于 2019-12-13 14:27:24

问题


i have two color "red" also "Salmon". i need create dynamiclly panel also panel background color. These colors must be between two color(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.


回答1:


As they have said you have an infinite recursive loop, and that's why you're getting the Stack Overflow.

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);
        }

That should stop the recursion. Its not very clear what you need beyond the dynamic panel creation. As is the code will just create one panel, and will create a new panel every time the BackGroundColorArranger is called -WITH A DIFFERENT COLOR PAIR- as you are using the colorpair as an ID for the panel.

If you need more than one panel make a finite loop calling BackGroundColorArranger with different pairs of colors ... if you need to actually see the panels on screen you'll need to change panel.Location of each in ArrangerBase, as now each panel starts at the origin with a fixed size.




回答2:


Because you call BackGroundColorArranger recursively with no exit condition. Here's a tip, when you get the stack overflow exception in the debugger, go to Debug Menu -> Windows -> Call Stack and you'll immediately see the problem.




回答3:


Not being C# developer, it looks to me like you have a simple endless recursion there.

Is this a joke question or what's the deal here?




回答4:


You're getting a stack overflow because each of these calls each other (which then calls the other, and so on until the stack overflows):

Your code is effectively:

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



回答5:


You are recursively calling the same function, the stock overflow error means your recursion is never ending.

You need to change the parameters so that when the method calls itself it will eventually find an end.



来源:https://stackoverflow.com/questions/985535/stack-overflow-error-on-color-changer-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!