VB.Net/Language Basics/Goto — различия между версиями

Материал из VB Эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 12:42, 26 мая 2010

Goto a Label

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


Goto Statement

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


On Error go to line

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


Use GOTO to deal with user input

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