Search

Feb 15, 2007

Pass arguments what requied [Microsoft.Design]

I come across one situation where I have to set visibility of panels, and I have to that code frequently so I created on function which accepts panels as argument…


public static void SetNoDataFound(Panel pnlToShow, Panel pnlToHide, Label txtToShowMessage, string strMessage)
{
pnlToShow.Visible = true;
pnlToHide.Visible = false;
txtToShowMessage.Text = string.Format(DefaultCulture, GetNoDataFoundMessage, strMessage);
}



Strange!! But it’s simply true. If you are using property of parent object then why should you pass child object?

So here is modified version of code, you can say optimized!!




public static void SetNoDataFound(Control pnlToShow, Control pnlToHide, Label txtToShowMessage, string strMessage)
{
pnlToShow.Visible = true;
pnlToHide.Visible = false;
txtToShowMessage.Text = string.Format(DefaultCulture, GetNoDataFoundMessage, strMessage);
}



You can see the change, instead of Panel I write Control which is parent of Panel.
This means that at the time of calling this function it’s just sent the reference of Control not Panel :)

No comments: