Results 1 to 9 of 9

Thread: [RESOLVED] there is no end if why

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2013
    Posts
    457

    Resolved [RESOLVED] there is no end if why

    Code:
     Public Function getkeystate(ByVal key1 As Integer) As Boolean
            Dim s As Short
            s = getasynckeystate(key1)
            If s = 0 Then Return False
            Return True
        End Function
    why is there no error for the missing end if? why is there no else statement?
    Thanks
    George

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    36,583

    Re: there is no end if why

    You don't need one in that case. I don't like writing If statements in that fashion, but if the block of the statement can be put on one line, then no End If is needed, and Else is ALWAYS optional.

    In this case, the block of code if the condition is true is just a single line, so you can write it either in it's own block:
    Code:
    If s=0 Then
      Return False
    End If
    or you can write it the way you have. It's been like that since the dawn of BASIC. In fact, it may have started with only the way you have shown, though I don't remember for sure. The If...End If block may have come later.

    As for Else, you never need it. In the case shown, there is no need for the Else. In fact, there is no need for the If itself, since this would be faster:
    Code:
    Return s<>0
    But if you want to use an If, you COULD use an Else, but in this case it gains you nothing.
    My usual boring signature: Nothing

  3. #3
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    10,474

    Re: [RESOLVED] there is no end if why

    For reference, here is the official documentation on the single line syntax: https://docs.microsoft.com/en-us/dot...le-line-syntax

    Keep in mind that programming in general, not just in Visual Basic .NET, is a very opinionated profession. Because there are multiple ways to skin a cat, there will inevitably be one group claiming that their way is better than the other. However, I tend to agree with Microsoft's remarks on the multiple-line syntax of the If...Then...Else statement:
    However, the multiple-line syntax provides more structure and flexibility and is easier to read, maintain, and debug.
    "Code is like humor. When you have to explain it, it’s bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2013
    Posts
    457

    Re: [RESOLVED] there is no end if why

    Thanks for the explanation
    George

  5. #5
    Fanatic Member
    Join Date
    Aug 2004
    Location
    Essex, UK
    Posts
    682

    Re: [RESOLVED] there is no end if why

    I would do it like this for best clarity:

    Code:
            If s = 0 Then
                Return False
            Else
                Return True
            End If

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    24,257

    Re: [RESOLVED] there is no end if why

    I’d agree with Shaggy’s last coding suggestion…
    Keep your code as concise and simple as you can. That’s my best advice.

  7. #7
    Fanatic Member
    Join Date
    Jan 2006
    Posts
    694

    Re: [RESOLVED] there is no end if why

    Why not keep in succinct?

    Code:
    Return s <> 0
    David Anton
    Convert between VB, C#, C++, & Java
    www.tangiblesoftwaresolutions.com

  8. #8
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    10,474

    Re: [RESOLVED] there is no end if why

    @David Anton - That was Shaggy's last suggestion that .paul. advocated for.
    "Code is like humor. When you have to explain it, it’s bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  9. #9
    Fanatic Member
    Join Date
    Jan 2006
    Posts
    694

    Re: [RESOLVED] there is no end if why

    Right - I read the thread too quick!
    David Anton
    Convert between VB, C#, C++, & Java
    www.tangiblesoftwaresolutions.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width