KevinUp

real world programming

Visible

Posted by kevinup on April 22, 2007

So I was working on this bug the other day. The ASP.net code looked something like this:
<asp :P anel ID =”Panel1″ runat =”server” Visible =”false“>
<
asp:Button ID =”Button1″ runat =”server” Visible =”true” />
</asp:Panel>
This is what the C# code looked like:

if (Button1.Visible)
{//do visible logic
}
else

{//do not visible logic
}

I was asking some of my developers which if-block should you fall into. Ironically of the 6 developers I asked they all said it should fall into the first if block. They were all wrong. Looking at part of the visible property in Reflector for .NET you can see what is going on:

 

public virtual bool Visible
{
    get
    {
        //…skipped this code
        if (this._parent != null)
        {
            return this._parent.Visible;
        }
        return true;
    }
}

As you can see the button inherits from its parent. I’ve been using .Net for a long time, and I can’t believe I haven’t run into this. It’s also interesting that no developer on my team answered this correctly. I suggested this would be a good interview question, but it was pointed out that no one would get it right.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>