Tim,
You would have to switch on the type name, which isn't truly accurate
(theoretically, you could have a type name which is the same between two
types, but they reside in different assemblies).
For comparisons against types, I would recommend using the if statement,
as you have there. You can't switch on reference types (with the exception
of strings) in a switch statement, so you will have to use the if statement.
To make it look better, I would recommend refactoring the code so that
it looks like this:
protected void SetThisControl( System.Web.UI.C ontrol WebControl1)
{
BaseValidationA ction(WebContro l1);
LabelAction(Web Control1);
ButtonAction(We bControl1);
DataGridAction( WebControl1);
}
And then, each function would look like this:
BaseValidationA ction(System.We b.UI.Control webControl)
{
// If the control is the right type, continue.
if (webControl as BaseValidator == null)
{
// Get out.
return;
}
}
If you want to get really fancy, I would name the methods in a manner
that relates to the type that is being processed, then use reflection to
make the call to the appropriate method (based on the type). This would
eliminate the need for the check in each function, and make the code in
SetThisControl more descriptive (instead of making needless calls).
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
-
mv*@spam.guard. caspershouse.co m
"Tim Cowan" <ti**********@p eopletogo.com> wrote in message
news:Ob******** ***********@new s20.bellglobal. com...
Hi
I was wondering if someone could help me change these ifs to a switch
statement. I tried but I can't make it work.
Thanks Tim
Code below:
protected void SetThisControl( System.Web.UI.C ontrol WebControl1)
{
if (WebControl1 is BaseValidator)
{
//Do stuff
}
if (WebControl1 is Label)
{
//Do something
}
if (WebControl1 is Button)
{
//Do something
}
if (WebControl1 is DataGrid)
{
//Do something
}
}