locked
Can someone give a good example of the GOTO statement in visual basic 2008?

    General discussion

  • I'm new to computer programming and I wanted a good example of the GOTO statement, I really want to know how to imply the goto statement in my future projects. It would be greatly appreiciated if someone can show me an example.
    Friday, January 15, 2010 2:17 AM

All replies

  • goto is evil try not to use it does not go well with event driven programming .
    Coding4fun Be a good forum member mark posts that contain the answers to your questions or those that are helpful
    Please format the code in your posts with the button . Makes it easier to read .
    Friday, January 15, 2010 2:21 AM
  • If anyone other than you will see your code,  don't use "GoTo".  Just the letters have a negative connotation.  There are many aliases that begin with "Exit" or "Continue" that you can use that are effectively "GoTo" in disguise.
    Friday, January 15, 2010 12:00 PM
  • Listen to the others. It's not sure if GoTo actually is evil, but it's pretty sure, that you'll become target of highest disdain when you use it :D. Just kidding. BTW, we all (GoTo believers and GoTo disbelievers) made at least consensus, that it's "considerably problematic" to use GoTo. Most of arguments against are pretty rational arguments, like "most of people would used GoTo too much, if you've told them thay can use it at all". It's simply true, that most of programmers should be just forced to believe in things like evil in GoTo, because really, it's kind of higgs-searching-science to find few places where GoTo is useful, practical and readable.

    I'll give you example of qbasic text-adventure, instead of probable "valid" visual basic GoTo samples, because saying they're valid could make some flamewar here. So take it just like "this was the way in qbasic to do an adventure, and it is also sample how you can (and shouldn't) use GoTo in most of *Basic languages"

    Dim i%
    
    InFrontOfCave:
    Input "Type 1 to go to the cave, type 0 to exit game"; i
    If i = 1 Then Goto Cave Else Goto ExitGame
    Print "Wrong input"
    GoTo InFrontOfCave
    
    Cave:
    Print "UR in cave"
    
    ExitGame:
    Print "Bye"
    End

    Best Regards, Matt
    If a <> b Then MsgBox(a): MsgBox(b) Else MsgBox(a) : End
    See also: 1, 2, 3, 4, 5, :)
    Friday, January 15, 2010 1:37 PM

  • You are standing at the end of a road before a small brick building.
    Around you is a forest.  A small stream flows out of the building and
    down a gully. In the distance there is a tall gleaming white tower.
    >enter

    You are inside a building, a well house for a large spring.
    There are some keys on the ground here.
    There is a shiny brass lamp nearby.
    There is food here.
    There is a bottle of water here.
    Looking for work - Zip 65101 http://www.vbforums.com/showthread.php?t=552774
    Friday, January 15, 2010 1:55 PM
  • My game is boring, I know. I've never written good game (2-3 in qbasic, but those were times I didn't know anything about arrays - I sufficed with For, Input, Print, Color, Line, GoTo and If).


    If a <> b Then MsgBox(a): MsgBox(b) Else MsgBox(a) : End
    See also: 1, 2, 3, 4, 5, :)
    Friday, January 15, 2010 2:43 PM
  • I'm new to computer programming and I wanted a good example of the GOTO statement, I really want to know how to imply the goto statement in my future projects. It would be greatly appreiciated if someone can show me an example.
    if texbox.text="Love: Then
    Goto Love

    else

    Goto Father

    End if

    Love:      msgbox("You typed Love:)")

    Father:   msgbox("nothing")

    Just Be Humble Malange!
    Friday, January 15, 2010 2:53 PM
  • I did it a long time ago:

    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
            On Error Resume Next
            If Button7.Text = "Query" Then

                MsgBox("Type in the correspoding box the information that you want to querie." & vbCrLf & _
                 "Example: ISBN: Book Numer " & vbCrLf & "Title: Book Title " & vbCrLf & "Author: Author Name", MsgBoxStyle.Information, "Choose only one boox")

                txtIsbn.Text = ""
                txtAuthor.Text = ""
                txtCopies.Text = ""
                txtGenre.Text = ""
                txtPublisher.Text = ""
                txtTitle.Text = ""
                txtCopies.Visible = False
                txtGenre.Visible = False
                txtPublisher.Visible = False
                txtIsbn.Focus()
                Button7.Text = ""
                Button7.Text = "Query Me"

            Else

                If txtIsbn.Text = "" Then
                    GoTo Line1
                Else

                    Dim isbquerie As String
                    isbquerie = ("SELECT BOOK.ISBN, BOOK.Title, BOOK.Author, BOOK.Publisher, BOOK.Copies, BOOK.Genre " & _
                                 "FROM BOOK " & _
                                 "WHERE BOOK.ISBN='" & txtIsbn.Text & "'")
                    BIRecord = New ADODB.Recordset
                    BIRecord.Open(isbquerie, Cnnn, CursorTypeEnum.adOpenStatic)

                    If BIRecord.EOF Then
                        MsgBox("The ISBN Not existe. Sorry " & vbCrLf & "Try by Author Name. Thanks", MsgBoxStyle.Information, "ISBN NOT Exist")

                        txtIsbn.Text = ""

                    Else
                        With BIRecord
                            txtCopies.Visible = True
                            txtGenre.Visible = True
                            txtPublisher.Visible = True
                            txtIsbn.Text = .Fields!ISBN.Value
                            txtAuthor.Text = .Fields!Author.Value
                            txtCopies.Text = .Fields!Copies.Value
                            txtGenre.Text = .Fields!Genre.Value
                            txtPublisher.Text = .Fields!publisher.Value
                            txtTitle.Text = .Fields!Title.Value
                            Button7.Text = "Query"
                            'Label24.Enabled = False
                        End With
                        Exit Sub
    Line1:              If txtAuthor.Text = "" Then
                            GoTo Line2
                        Else
                            isbquerie = ("SELECT BOOK.Author, BOOK.ISBN, BOOK.Title, BOOK.Publisher, BOOK.Copies, BOOK.Genre " & _
                                       "FROM BOOK " & _
                                       "WHERE BOOK.Author='" & txtAuthor.Text & "'")
                            BIRecord = New ADODB.Recordset
                            BIRecord.Open(isbquerie, Cnnn, CursorTypeEnum.adOpenStatic)

                            If BIRecord.EOF Then
                                MsgBox("The Author Not existe. Sorry " & vbCrLf & "Try by Author Name. Thanks", MsgBoxStyle.Information, "ISBN NOT Exist")

                                txtAuthor.Text = ""
                            Else
                                With BIRecord
                                    txtCopies.Visible = True
                                    txtGenre.Visible = True
                                    txtPublisher.Visible = True
                                    txtIsbn.Text = .Fields!ISBN.Value
                                    txtAuthor.Text = .Fields!Author.Value
                                    txtTitle.Text = .Fields!Title.Value
                                    txtCopies.Text = .Fields!Copies.Value
                                    txtGenre.Text = .Fields!Genre.Value
                                    txtPublisher.Text = .Fields!publisher.Value
                                    Button7.Text = "Query"
                                    'Label24.Enabled = False
                                End With
                                Exit Sub
    Line2:                      If txtTitle.Text = "" Then
                                    GoTo Line3
                                Else

                                End If
                                isbquerie = ("SELECT BOOK.Title, BOOK.Author, BOOK.ISBN, BOOK.Publisher, BOOK.Copies, BOOK.Genre " & _
                                    "FROM BOOK " & _
                                    "WHERE BOOK.Title='" & txtTitle.Text & "'")
                                BIRecord = New ADODB.Recordset
                                BIRecord.Open(isbquerie, Cnnn, CursorTypeEnum.adOpenStatic)

                                If BIRecord.EOF Then
                                    MsgBox("The Title Not existe. Sorry " & vbCrLf & "Try by Author Name. Thanks", MsgBoxStyle.Information, "ISBN NOT Exist")

                                    txtTitle.Text = ""
                                Else
                                    With BIRecord
                                        txtCopies.Visible = True
                                        txtGenre.Visible = True
                                        txtPublisher.Visible = True
                                        txtIsbn.Text = .Fields!ISBN.Value
                                        txtAuthor.Text = .Fields!Author.Value
                                        txtCopies.Text = .Fields!Copies.Value
                                        txtGenre.Text = .Fields!Genre.Value
                                        txtPublisher.Text = .Fields!publisher.Value
                                        txtTitle.Text = .Fields!Title.Value
                                        Button7.Text = "Query"
                                        'Label24.Enabled = False
                                    End With
                                    Exit Sub
                                End If
                            End If
                        End If
                    End If
                End If
                End If

    line3:  MsgBox("Type in the correspoding box the information that you want to querie." & vbCrLf & _
          "Example: ISBN: Book Numer " & vbCrLf & "Title: Book Title " & vbCrLf & "Author: Author Name", MsgBoxStyle.Information, "Choose only one boox")


        End Sub


    Just Be Humble Malange!
    Friday, January 15, 2010 2:53 PM