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:Panel 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.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

 
%d bloggers like this: