Querying for used IdSymbol text strings

I have a program(working) that places an Id symbol on a drawing and fills in the upper text as A00x where A is the computed height coordinate, 00 is the computed length coordinate and x is a manually edited number between 0 and 9 to differentiate between symbols in the same grid coordinates.

What I would like to accomplish is automatically filling in the differentiating digit by checking the drawing to see if it has been used before, ie H140 and H141 already exist, so use H142. How would you format the code to test if a string + number combination already exists, if it doesn't then use that combination and if it does try the next value up?

...

Dim idText As String
idText = yname & xname
Dim symbolText As String
Try 'This does not work
Dim idsym As Annotations.IdSymbolCollection = theSession.Parts.Work.Annotations.IdSymbols
If idsym.UpperText <> idText & 0 Then
symbolText = idText & 0
Else If idsym.UpperText <> idText & 1 Then
symbolText = idText & 1
End If
Catch ex As NXOpen.NXException
UI.GetUI().NXMessageBox.Show("Message", NXMessageBox.DialogType.[Error], ex.Message)
End Try

Try this:


Option Strict Off
Imports System
Imports System.Collections.Generic
Imports NXOpen

Module Module1

Sub Main()

Dim theSession As Session = Session.GetSession()

Dim symbolLocations As New List(Of String)
'gather existing symbol text into the list

'the following is for illustration only, in practice you will
'want to loop through the collection of symbols and add
'the appropriate text items
symbolLocations.Add("H140")
symbolLocations.Add("H141")

Dim symbolZone As String = "H14"
Dim nextSymbol As String = ""

Dim symbolNum As Integer = NextAvailableNumber(symbolZone, symbolLocations)
If symbolNum = -1 Then
MsgBox("that zone already contains 10 symbols!")
Else
nextSymbol = symbolZone & symbolNum.ToString
symbolLocations.Add(nextSymbol)
MsgBox("your next symbol text is: " & nextSymbol)
End If

End Sub

Function NextAvailableNumber(ByVal zone As String, ByVal idList As List(Of String)) As Integer

For i As Integer = 0 To 9
If Not idList.Contains(zone & i.ToString) Then
'this number is available
Return i
End If
Next

'we've cycled through numbers 0 to 9, all were taken
'return -1 as a flag that 0-9 were taken
'alternately you could throw an exception
Return -1

End Function

End Module