Stack Overflow Error in UserControl

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Harvey

    Stack Overflow Error in UserControl

    Hello:

    I would appreciate if anyone could tell me why the
    following occurred in my WinForms app. I am using a User
    control and everything was running ok until I defined the
    following property:

    public int Height
    // Height of the control in pixels
    {
    get
    {
    return this.Height;
    }
    set
    {
    this.Height = value;
    }
    }


    Then, when I made the following call:

    this.Height = Math.Max((ctl.T op + ctl.Height),
    this.Height);


    the stack overflow error would reproducibly occur. Yet
    the app compiled without error.

    Could someone explain what is going on here? I imagine it
    has something to do with property overriding, but if that
    is the case, why is not a compilation error thrown?

    thanks in advance.

    Regards,
    Harvey
  • Tobin Harris

    #2
    Re: Stack Overflow Error in UserControl

    "Harvey" <hflaishe@polar is.umuc.edu> wrote in message
    news:087e01c39f e4$ea27c2a0$a00 1280a@phx.gbl.. .[color=blue]
    > Could someone explain what is going on here? I imagine it
    > has something to do with property overriding, but if that
    > is the case, why is not a compilation error thrown?[/color]

    I imagine that the "Height" get property calling itself when you return
    this.Height. Therefore, the execution falls into a recursive loop that
    cannot exit (until it finally overflows the stack causing an exception!).
    Guessing a little, but one idea might be to have your get property call
    "base.Heigh t" rather than "this.Heigh t"? Just out of interest, why are you
    overriding this property when all it does is what the base class does!?

    Hope this helps.

    Tobin


    Comment

    Working...