IF THEN & this & that etc before ELSE ...VB6 (Newby)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Tomservo
    New Member
    • Mar 2007
    • 23

    IF THEN & this & that etc before ELSE ...VB6 (Newby)

    Hi Guys,
    I've looked for this and can't find anything relative.
    My problem is this :-
    I want to set more than one value as the result of an IF function, (I seem to remember being able to do this with BBC Basic) but everything I've tried returns an error.
    can someone point me in the right direction?
    regards
    Last edited by Tomservo; Mar 12 '07, 09:54 AM. Reason: punctuation
  • mabubakarpk
    New Member
    • Feb 2007
    • 62

    #2
    You can follow this code

    Code:
    ****************Simple If End If**************************
     if condition then
        statement
     else
       statement
     end if
    ****************Multiple If **********************
     if condition then
       statement
     elseif condition then
       statement
     elseif condition then
      statement
     else
      statement
     end if
    
    ************************************************

    Comment

    • Tomservo
      New Member
      • Mar 2007
      • 23

      #3
      Thankyou! I'll give it a go. while Im on, every time I finish with 'End If', when I run the program It stops at that point with the statement "Compile error: End If without block If", having said that it seems to run ok without putting it in...what am I missing?
      regards

      Comment

      • willakawill
        Top Contributor
        • Oct 2006
        • 1646

        #4
        Hi. a couple of things. Firstly it may be better for you to use a select case statement if you are checking only one variable as in:
        Code:
        Select Case TheVariable
           Case 1
              'do something
        
           Case 2
              'do something
        
           Case 3
              'do something
        
           Case 4
              'do something
        
           Case Else
              'do something
        End Select
        Secondly, regarding the problem with your if statement, you need to post that code here so we can go over it together.

        Comment

        • Tomservo
          New Member
          • Mar 2007
          • 23

          #5
          Brilliant!
          I didn't know there was a 'Case' statement
          Thanks that'll sort it.

          Here's the code thats doesn't seem to be going by the book.


          ‘Cill is the Checkbox


          (This Works)

          If Cill.Value = 1 Then Height = Height - 30 _
          Else: Height = Height



          (This Don’t)

          If Cill.Value = 1 Then Height = Height - 30 _
          Else: Height = Height _
          End If

          regards

          Comment

          • willakawill
            Top Contributor
            • Oct 2006
            • 1646

            #6
            The part that doesn't work does not need an End If statement because you have put the code inline and not on a separate line.

            Comment

            • Killer42
              Recognized Expert Expert
              • Oct 2006
              • 8429

              #7
              Originally posted by Tomservo
              ...
              If Cill.Value = 1 Then Height = Height - 30 _
              Else: Height = Height _
              End If
              You've got some mighty strange coding habits going, there. What you've done is to write a complex single-line statement, and break it over multiple lines. In other words, thanks to the "_" continuation characters, your single statement actually says
              Code:
              If Cill.Value = 1 Then Height = Height - 30 Else: Height = Height [B]End If[/B]
              Trust me, there is no longer any need to try and cram multiple statements into a single line like this. I highly recommend you consider the following changes.
              • As willakawill pointed out, you don't use End If for a single-line or "inline" If statement.
              • Lose the colon. Very rarely needed in VB, and in this case completely unnecessary
              • Split your If statement and indent the dependent code - much easier to read.
              • Height = Height ??? What the... :confused:

              The result would look something like this...
              Code:
              If Cill.Value = 1 Then
                Height = Height - 30
              Else
                Height = Height
              End If

              Comment

              • knychtell
                New Member
                • Jan 2007
                • 66

                #8
                [maybe you can add this

                Do
                if condition then
                codes
                end if
                exit do
                ...
                ...
                ...
                ...
                .

                until condition
                if a certain condition was met in a if statement it will run that particular statement, if not it will go to exit then to next if.....

                i hope this could help ya....

                Comment

                • Killer42
                  Recognized Expert Expert
                  • Oct 2006
                  • 8429

                  #9
                  Yes, I suppose if you can't be bothered writing a proper structure you can always fall back on the old GoTo statement - or code a more complex equivalent as in this case .

                  Comment

                  • Tomservo
                    New Member
                    • Mar 2007
                    • 23

                    #10
                    Wow!!
                    Ok Thanks Guys, I've embarrassed myself again..oh well

                    in my defence I picked up the book about 3 days ago and ended up realising its the wrong one (Visual Basic 2005 for Dummies) so that ended up in the bin.
                    I've been using the Internet for reference, and a very basic knowledge of BBC Basic 25 years ago.
                    by the way I didn't put the Colon in, I watched as it was added automatically.

                    Thanks all of you for helping, I've learnt a lot from it and Its helping me a lot, I'm not giving up!

                    Best regards

                    Comment

                    • Tomservo
                      New Member
                      • Mar 2007
                      • 23

                      #11
                      Originally posted by willakawill
                      The part that doesn't work does not need an End If statement because you have put the code inline and not on a separate line.
                      Thanks willakawill, I've grasped it now, I've realised that the "Then" statement has to be on the "If" line; I was putting it on the next line and that was screwing everything else up, unless I used the Underscore; Now its in the right place I'm making sense out of everything you've said.

                      Comment

                      • Tomservo
                        New Member
                        • Mar 2007
                        • 23

                        #12
                        Originally posted by Killer42
                        You've got some mighty strange coding habits going, there. What you've done is to write a complex single-line statement, and break it over multiple lines. In other words, thanks to the "_" continuation characters, your single statement actually says
                        Code:
                        If Cill.Value = 1 Then Height = Height - 30 Else: Height = Height [B]End If[/B]
                        Trust me, there is no longer any need to try and cram multiple statements into a single line like this. I highly recommend you consider the following changes.
                        • As willakawill pointed out, you don't use End If for a single-line or "inline" If statement.
                        • Lose the colon. Very rarely needed in VB, and in this case completely unnecessary
                        • Split your If statement and indent the dependent code - much easier to read.
                        • Height = Height ??? What the... :confused:

                        The result would look something like this...
                        Code:
                        If Cill.Value = 1 Then
                          Height = Height - 30
                        Else
                          Height = Height
                        End If
                        Thanks Killer42
                        I was looking at your code when I realised what I was doing wrong; I've been assuming that the "If & Then" statements could be put on seperate lines, which dosent work, and then found that if I put everything on one line it worked; having read that it's recommended to keep the lines short, I broke it up with the Underscore and then had a problem with "End If".
                        Now I know to finish the "If" line with "Then", everything else falls into place.
                        Thankyou!
                        ("Height" is a variable defined by an input in the window frame pricing prog I'm trying to make, - 30 is the thickness of the Cill if required)

                        Comment

                        • Killer42
                          Recognized Expert Expert
                          • Oct 2006
                          • 8429

                          #13
                          Originally posted by Tomservo
                          Thanks Killer42
                          I was looking at your code when I realised what I was doing wrong; I've been assuming that the "If & Then" statements could be put on seperate lines, which dosent work, and then found that if I put everything on one line it worked; having read that it's recommended to keep the lines short, I broke it up with the Underscore and then had a problem with "End If".
                          Now I know to finish the "If" line with "Then", everything else falls into place.
                          Thankyou!
                          ("Height" is a variable defined by an input in the window frame pricing prog I'm trying to make, - 30 is the thickness of the Cill if required)
                          It sounds as though you're doing fine - you're not embarrassing yourself. Just keep in mind...
                          1. There's no such thing as a "Then statement". It's just a required part of the If statement.
                          2. There are subtle differences between a single-line statement and a multi-line "statement block". Packing multiple statements onto a line just confuses the issue. It's usually (though not necessarily always) simpler, cleaner and easier to read if you stick to a single statement per line.
                          3. The underscore simply allows you to visually break up a long statement so that you can see it all without scrolling all over the place. It is still treated as a single statement. For example, to take it to ridiculous extremes you could calculate 2 + 3 like so...
                            Code:
                            A _
                            = _
                            2 _
                            + _
                            3
                          4. The point I was making about Height is just that...
                            Code:
                            Else
                              Height = Height
                            does absolutely nothing. But uses up some processor cycles to do it. :)

                          Comment

                          • Tomservo
                            New Member
                            • Mar 2007
                            • 23

                            #14
                            Originally posted by Killer42
                            It sounds as though you're doing fine - you're not embarrassing yourself. Just keep in mind...
                            1. There's no such thing as a "Then statement". It's just a required part of the If statement.
                            2. There are subtle differences between a single-line statement and a multi-line "statement block". Packing multiple statements onto a line just confuses the issue. It's usually (though not necessarily always) simpler, cleaner and easier to read if you stick to a single statement per line.
                            3. The underscore simply allows you to visually break up a long statement so that you can see it all without scrolling all over the place. It is still treated as a single statement. For example, to take it to ridiculous extremes you could calculate 2 + 3 like so...
                              Code:
                              A _
                              = _
                              2 _
                              + _
                              3
                            4. The point I was making about Height is just that...
                              Code:
                              Else
                                Height = Height
                              does absolutely nothing. But uses up some processor cycles to do it. :)
                            I appreciate the help;
                            Does this mean that you can use 'If & Then' without the 'Else'?, or put the 'Else' in without specifying anything after it?; I've been concerned that unless I tell it to revert back to the former value of "Height" for any other condition, it will remain the value of the 'If' line.
                            regards

                            Comment

                            • Killer42
                              Recognized Expert Expert
                              • Oct 2006
                              • 8429

                              #15
                              Originally posted by Tomservo
                              I appreciate the help;
                              Does this mean that you can use 'If & Then' without the 'Else'?, or put the 'Else' in without specifying anything after it?; I've been concerned that unless I tell it to revert back to the former value of "Height" for any other condition, it will remain the value of the 'If' line.
                              regards
                              • The Else is completely optional on an If statement. If you don't use it, you are effectively saying "Else, do nothing".
                              • You cannot simply write an Else statement - it has to follow an If.
                              • If the If condition is not true, then nothing will happen.

                              It might be a good idea to play with the whole If structure for a while, just to ensure that you're completely comfortable with it. Test before and after values, that sort of thing. I think this would be time well spent, as it's a critically important programming structure that you will use a lot if you go continue coding.

                              Don't forget, VB has an excellent debugging facility which allows you to stop and continue the code, examine and change values on the fly, and so on. You should get into the habit of using the debugging functions, they'll make your life much easier.

                              Also, make sure you're familiar with the notation used in the manual/help to describe statement syntax, so you can identify the optional parts, repeating parts, etc.

                              Comment

                              Working...