VB.Net/Language Basics/Goto

Материал из VB Эксперт
Версия от 15:42, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Goto a Label

<source lang="vbnet"> Imports System public class MainClass

   Shared Sub Main()
      Dim counterVariable As Integer = 0
repeat:  " the label
      Console.WriteLine("counterVariable: {0}", counterVariable)
      " increment the counter
      counterVariable += 1
      If counterVariable < 10 Then
         GoTo repeat " the dastardly deed
      End If
   End Sub

End Class


      </source>


Goto Statement

<source lang="vbnet"> Imports System Public Class MainClass

   Shared Sub Main(ByVal args As String())
      Dim counterVariable As Integer = 0

repeat: " the label

      Console.WriteLine("counterVariable: {0}", counterVariable)
      " increment the counter
      counterVariable += 1
      If counterVariable < 10 Then
         GoTo repeat " the dastardly deed
      End If
   End Sub

End Class

      </source>


On Error go to line

<source lang="vbnet"> Imports System Public Class MainClass

   Shared Sub Main(ByVal args As String())
       On Error GoTo LoadError
       LoadPayrollFile()
       On Error GoTo PrintError
       PrintPaychecks()
       Exit Sub

LoadError:

       Console.WriteLine("Error loading the file.")
       Exit Sub

PrintError:

       Console.WriteLine("Error printing.")
       Exit Sub
   End Sub
   Shared Private Sub LoadPayrollFile()
       Throw New ArgumentException("Error in subroutine LoadFile")
   End Sub
   Shared Private Sub PrintPaychecks()
       Throw New ArgumentException("Error in subroutine Print")
   End Sub

End Class

      </source>


Use GOTO to deal with user input

<source lang="vbnet"> Imports System

Public Class MainClass

  Shared Sub Main()
       Dim getData As String
       Dim i, j As Integer
       For i = 1 To 10
           For j = 1 To 100
               Console.Write("Type the data, hit the Enter key between " & _
                   "Q to end:  ")
               getData = Console.ReadLine()
               If getData = "Q" Then
                   GoTo BadInput
               Else
                   "Process data
               End If
           Next j
       Next i
       Exit Sub

BadInput:

       Console.WriteLine("Data entry ended at user request")
  End Sub 

End Class


      </source>