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

Shorten this conditional Statement

Is there a way to convert this to a shorter condition with OR's? I tried and got flakey results.
If tb.Text = "" Then
      If tb.Name = "textbox1" Then
      ElseIf tb.Name = "textbox2" Then
      ElseIf tb.Name = "textbox3" Then
      Else
           tb.Text = "0"
      End If
      bool = False
End If

Open in new window

0
bertino12
Asked:
bertino12
  • 2
  • 2
1 Solution
 
mds-cosCommented:
I would personally use a CASE statement for this (structured programming background), but don't see anything wrong with what you are doing.  Why do you want to shorten it?

If tb.Text = "" Then
      SELECT CASE tb.Name
             CASE "textbox1"
                    ......
             CASE "textbox2"
                   ......
             CASE "textbox3"
                  .......
             CASE Else
                  tb.Text = "0"
      END SELECT
       bool = False
End If
0
 
mds-cosCommented:
Ummm....oops!  I may have misread the intent of your code....did you simply omit the code that would normally follow "THEN"?

If not, try this....

 If tb.Text = "" Then
      If tb.Name <> "textbox1" and tb.Name <> "textbox2" and tb.Name <> "textbox3" Then
           tb.Text = "0"
      End If
      bool = False
End If
0
 
käµfm³d 👽Commented:
What about:
If tb.Text = "" AndAlso (tb.Name = "textbox1" OrElse tb.Name = "textbox3" OrElse tb.Name = "textbox3") Then
    tb.Text = "0"
    bool = False
End If

Open in new window

0
 
käµfm³d 👽Commented:
Second one should be "textbox2". Sorry :\
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now