• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 353
  • Last Modified:

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

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

0
megnin
Asked:
megnin
  • 4
  • 4
  • 4
  • +1
1 Solution
 
athomsfereCommented:
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
0
 
Jeff CertainCommented:
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.
0
 
Jeff CertainCommented:
@athomsfere: Switch statements don't work for type comparison.
0
 
megninAuthor 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.
0
 
käµfm³d 👽Commented:
I suggest using "OrElse" for its short-circuit effect.

In addition to the switch that athomsfere mentioned, the only thing I can think of would be to keep the types in a list and check that:
Private Sub IterateControls(ByVal control As Control)
    Dim types As New List(Of Type)(New Type() {GetType(TextBox), GetType(RadioButtonList), GetType(Label), GetType(HtmlInputText), GetType(HtmlSelect), GetType(DropDownList), GetType(CheckBox), GetType(Button)})

    For Each c As Control In control.Controls
        If types.Contains(c.GetType()) Then
            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

0
 
Jeff CertainCommented:
@kaufmed, Agreed on OrElse. Good suggestion. :)
0
 
käµfm³d 👽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

0
 
megninAuthor 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.
0
 
Jeff CertainCommented:
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
0
 
käµfm³d 👽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.
0
 
megninAuthor Commented:
I asked "Support" to open the question back up for me so I can distribute the points on the "newly submitted" good solutions.
0
 
käµfm³d 👽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

0
 
megninAuthor Commented:
Excellent solution.  Thanks a lot!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 4
  • 4
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now