How to change these ifs to switch

  • Thread starter Thread starter Tim Cowan
  • Start date Start date
T

Tim Cowan

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.Control 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

}


}
 
Tim said:
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.
as i know switch accepts integral type only,
if not this code should work i think, but as i said earlier I DONT THINK
this is possible,

switch(WebControl1.GetType())
{
case typeof(System.Web.UI.WebControls.BaseValidator):
MessageBox.Show("this is web validator");
case etc...

}
 
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.Control WebControl1)
{
BaseValidationAction(WebControl1);
LabelAction(WebControl1);
ButtonAction(WebControl1);
DataGridAction(WebControl1);
}

And then, each function would look like this:

BaseValidationAction(System.Web.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.
 
sorry there was a problem at my previouse message

as i know switch accepts integral type only,
if not this code should work i think, but as i said earlier I DONT THINK
this is possible,

switch(WebControl1.GetType())
{
case typeof(System.Web.UI.WebControls.BaseValidator):
MessageBox.Show("this is web validator");
case etc...

}



Erdem
 
Tim Cowan said:
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.Control 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

}


}
Here's one possibility (there may well be better ones).
The WebControl class has a GetType method available that will return an
instance of class Type indicating the type of control in question. In turn,
class Type exposes a property FullName which gives you the name of the
control type as a String. More specifically, it :
<quote>
Gets the fully qualified name of the Type, including the namespace of the
Type.

</quote>

As the C# switch construct can accept either integral value or string type
expressions as the switch expression, you should be able to use the FullName
property (or some portion thereof) as the switch expression.

Hopefully, there's a simpler way and somebody else will post it :-)
 
Tim Cowan said:
I was wondering if someone could help me
change these ifs to a switch statement.
I tried but I can't make it work.

You'd need to do something like 'switch (ctl.GetType())', but that's
not permitted because data types are not integral types.

If the set of ifs is a real problem for you, polymorphism is an
option. You could inherit from the necessary controls, make your
inherited versions implement an interface ISomething (with a method M
that does whatever your current code is trying to do to each control),
pass your control as an ISomething, and call M on it.

P.
 
Peter van der Goes said:
Here's one possibility (there may well be better ones).
The WebControl class has a GetType method available that will return an
instance of class Type indicating the type of control in question. In turn,
class Type exposes a property FullName which gives you the name of the
control type as a String. More specifically, it :
<quote>
Gets the fully qualified name of the Type, including the namespace of the
Type.

</quote>

As the C# switch construct can accept either integral value or string type
expressions as the switch expression, you should be able to use the FullName
property (or some portion thereof) as the switch expression.

Hopefully, there's a simpler way and somebody else will post it :-)
FWIW, I just ginned up a little test app using the technique above and it
works fine.
The string returned from the FullName property is like:
"System.Web.UI.WebControls.Label" for a label.
First create an instance of class Type to hold the results of the call to
the GetType method, then capture the FullName property of your Type object
in a string
Example:

Type a;

a = CheckBox1.GetType();

String aa = a.FullName;

Peter [MVP Visual Developer]
Jack of all trades, master of none.
 
Actually, it would be impossible (*) to replace it with a switch
statement. The problem is the line:
if (WebControl1 is BaseValidator)
Now, since BaseValidator is an abstract class WebControl1 cannot be a
BaseValidator; it has to be a derived class. So if you were to do what
other here suggest, namely:

switch(WebControl1.GetType().FullName)
case "System.Web.UI.WebControls.BaseValidator":

You'll never get a match.

Continue with the if() statements. That is precisely what the "is" operator
is designed for.


(*) OK, a possibly sufficient approximation IS possible, but it's ugly:
switch(WebControl1.GetType().FullName)
case "System.Web.UI.WebControls.RegularExpressionValidator":
case "System.Web.UI.WebControls.RequiredFieldValidator":
case "System.Web.UI.WebControls.CompareValidator":
case "System.Web.UI.WebControls.RangeValidator":
/* Be sure to include cases for any class you derived from
CustomValidator
*/
/*
Do Stuff
*/


--
Truth,
James Curran
[erstwhile VC++ MVP]
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
 
Back
Top