' Collection of code snippets by Arne Vajhøj 
' (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
﻿Imports System

Namespace GoToDemo
    Public NotInheritable Class Luck
        Private Shared rng As New Random()
        Public Shared Function InLuck() As Boolean
            Return rng.NextDouble() > 0.2
        End Function
    End Class
    Public Class X
        Private id As String
        Private Sub New(id As String)
            Me.id = id
            Console.WriteLine("X {0} created", id)
        End Sub
        Public Shared Function Create(id As String) As X
            If Luck.InLuck() Then
                Return New X(id)
            Else
                Return Nothing
            End If
        End Function
        Public Sub Close()
            Console.WriteLine("X {0} closed", id)
        End Sub
    End Class
    Public Class Program
        Public Shared Sub Main(args As String())
            Dim o1 As X = X.Create("A")
            If o1 Is Nothing Then
                Console.WriteLine("Error creating A")
                GoTo lbl_done
            End If
            Dim o2 As X = X.Create("B")
            If o2 Is Nothing Then
                Console.WriteLine("Error creating B")
                GoTo lbl_close1
            End If
            ' 20 lines
            Console.WriteLine("Part 1")
            If Not Luck.InLuck() Then
                Console.WriteLine("Error during execution")
                GoTo lbl_close2
            End If
            ' 20 lines
            Console.WriteLine("Part 2")
        lbl_close2:
            o2.Close()
        lbl_close1:
            o1.Close()
        lbl_done:
            ' done
        End Sub
    End Class
End Namespace
