One line IF statements

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Dan2kx
    Contributor
    • Oct 2007
    • 365

    One line IF statements

    Just a quick question....
    Not really a fundamental one, just a general yes or no...

    ya know how ifs work...
    Code:
    If Something = True Then 
         Do something
    End if
    Or
    Code:
    If Something = True Then Do something
    is it possible to construct a "queue" of things to do in a single line?
    for example
    Code:
    If Something = True Then Do something; Do something else
    Just curious, i often prefer the one line IF for short IF's (i think it looks tidier?)

    Dan
  • patjones
    Recognized Expert Contributor
    • Jun 2007
    • 931

    #2
    Dan -

    It's possible, by separating the statements to be executed with a ":" -

    Code:
    If [B][I]some condition true[/I][/B] Then [B][I]statement1 : statement2 : ... : statementN[/I][/B]

    But this is a good idea only for simple If constructs. For anything with more than one or two statements it is usually more readable to use the regular "block" form -

    Code:
    If [B][I]some condition true[/I][/B] Then
    [B][I]     statement1
         statement2
         ...
         statementN[/I][/B]
    End If

    Pat
    Last edited by patjones; Mar 8 '10, 09:56 PM. Reason: code correction

    Comment

    • Dan2kx
      Contributor
      • Oct 2007
      • 365

      #3
      oh yeah i agree, complex things needthe full construct.

      i just found my self writing two almost identical lines which were very basic and thought.... hmmmmmmmmmmmm.. . i wonder IF

      Cheers

      Comment

      • tasawer
        New Member
        • Aug 2009
        • 106

        #4
        Hi,

        have you considered using SELECT CASE?

        Code:
        SELECT CASE x
         Case x=1st option
         Do Something
        
         Case x=2nd Option
         Do Something
        
        etc.
        
        END SELECT

        Comment

        • Dan2kx
          Contributor
          • Oct 2007
          • 365

          #5
          I use select case statements regularly, my needs here are just to tidy up small simple statements, like procedure calls etc.

          Dan

          Comment

          Working...