Try this: Sub RunScript(ByVal ptList As List(Of On3dPoint))
Dim tree As New DataTree(Of On3dPoint)
Dim pathNum As Int32 = 0
Dim j As Int32 = ptList.Count() - 1
nearTwo(ptList, tree, pathNum, j)
pOut = tree
End Sub
#Region "Additional methods and Type declarations"
Sub nearTwo (ByVal ptsIn As List(Of On3dPoint), ByRef tree As DataTree(Of On3dPoint), ByRef pathNum As Int32, ByRef j As Int32)
While (j > 3)
Dim path As New EH_Path(pathNum)
Dim d1 As Double = Double.MaxValue
Dim d2 As Double = Double.MaxValue
Dim min1 As Int32 = -1
Dim min2 As Int32 = -1
For i As Int32 = 1 To j - 1
Dim d As Double = ptsIn(0).DistanceTo(ptsIn(i))
If (d < d1) Then
d1 = d
min1 = i
Else If (d < d2) Then
d2 = d
min2 = i
End If
Next