We help IT Professionals succeed at work.

Shorthand for multiple If condition or condition or condition then statement?

megnin
megnin asked
on
Medium Priority
364 Views
Last Modified: 2012-05-11
Is there a "shorthand" for doing multipl "or"s like this?

I saw an examle  of *something* that did this:
If TypeOf c Is TextBox, RadioButtonList, Label Then

But that didn't work.  Is there a similar syntax for this?

Thanks.
Private Sub IterateControls(ByVal control As Control)
        For Each c As Control In control.Controls
            If TypeOf c Is TextBox _
            Or TypeOf c Is RadioButtonList _
            Or TypeOf c Is Label _
            Or TypeOf c Is HtmlInputText _
            Or TypeOf c Is HtmlSelect _
            Or TypeOf c Is DropDownList _
            Or TypeOf c Is CheckBox Then
                '            Or TypeOf c Is Button _
                Response.Write(c.ID.ToString & "<br />")
            End If
            If c.Controls.Count > 0 Then
                IterateControls(c)
            End If
        Next
        Response.Write(control.Controls.Count.ToString)
    End Sub

Open in new window

Comment
Watch Question

I am unsure why you need to programatically find the type of control, but a switch statement is similar to what you have there:

http://stackoverflow.com/questions/820104/vb-net-switch-statement-goto-case
There's no alternative to this that I'm aware of. (I'd be delighted to be proven wrong, though!) You could do something funky like getting the string representation of the type name, then using a Select Case statement, but that'd be a whole bunch of badness for no good reason.

If this is a check you need to do frequently, you could wrap it in a method, or encapsulate the functionality in an extension method on Object.
@athomsfere: Switch statements don't work for type comparison.

Author

Commented:
Thanks for the replys.  I guess I can live with it.  I only use this to get a list of controls on a page so I can see If I've missed any in a mass renaming and so I have a list of controls that I need to create database fields for and to have a list I can use in creating the SQL Insert and Update statements.

I had this list of, what, 8 control types and I wanted to comment a few of them out, but with the line continuation _
character _
I couldn't just comment out the line I didn't want to use I had to cut them out of the for each and move them somewhere else or just delete them.  A comment in the middle broke it.

I'll split the points between you.
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
@kaufmed, Agreed on OrElse. Good suggestion. :)
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015

Commented:
@Chaosian

@athomsfere: Switch statements don't work for type comparison.
Says who?
Select Case True
    Case TypeOf (c) Is TextBox, TypeOf (c) Is RadioButtonList, etc.

End Select

Open in new window

Author

Commented:
Hey kaufmed, I'm going to request from Admin that I be allowed to redistribute the points.  I was looking for a way to use a list as I noted in my question.

Sorry, Chaosian and athomsfere; kaufmed provided the answer I was looking for.
Kaufmed,

That's a clever way to pervert the syntax. What I meant was This doesn't work:

Select Case TypeOf(c)
    Case Is TextBox, etc
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015

Commented:
For future readers, the above SELECT won't work in C# (switch). "Select True" is something that's VB-specific and each of the "cases" in a C# switch must be constant values. Trying to call the typeof operator in a case would be interpreted as a variable value and you would receive a compiler error.

Author

Commented:
I asked "Support" to open the question back up for me so I can distribute the points on the "newly submitted" good solutions.
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015

Commented:
@Chaosian

What I meant was This doesn't work
Agreed. Which is why I didn't post it   ; )

What's amusing about that, though, is that the "Is" keyword actually can be used in a Case statement, just not for types.

E.G.
Dim maxNumber As Integer = 500

Select Case 
    Case 1 To 4, 7 To 9, 11, 13, Is > maxNumber
...
End Select

Open in new window

Author

Commented:
Excellent solution.  Thanks a lot!
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.