I have an Array of chars, which is completely random except that every character occurs at most once.
I also have a string containing only characters that are present in the array. I want this string to be "counted upwards" (if that makes sense), like "111" becomes "112", or "aaa" becomes "aab".
Let´s say the Array of chars contains 1, 2, and 3. The example above works, but when the string is "333" (should become "1111"), my function returns an empty - or wrong - string.
If I call the function again, providing the wrong string, after a few times it returns the correct value ("1111").
Why does this happen?
This is my function:
Public Function getNextString(ByVal currentStr As String, ByVal pattern() As Char) As String 
    'currentStr is the string which I want to count upwards, pattern() is the array of chars
    Dim nextStr As String = ""
    Dim currentStrArray() As Char = currentStr.ToCharArray
    Dim currenStrPosition As Integer = currentStrArray.Length - 1
    Dim finished As Boolean = False
    Do Until finished = True
        Dim newPosition As Integer = getPositionInArray(currentStrArray(currentStrPosition)) 'this is a custom function, should be self-explaining
        If newPosition = Nothing Then Return Nothing
        newPosition += 1
        Try
            currentStrArray(currenStrPosition) = pattern(newPosition)
            finished = True
        Catch ex As IndexOutOfRangeException
            currentStrArray(currentStrPosition) = pattern(0)
            currentStrPosition -= 1
        End Try
        If currentStrPosition < 0 Then
            nextStr = pattern(0)
            finished = True
        End If
    Loop
    For i As Integer = 0 To currentStrArray.Length - 1
        nextStr = nextStr & currentStrArray(i)
    Next
    Return nextStr
End Function
Any ideas?
EDIT:
As an example, I have the array {"1","2","3"}. My string is first "111". I want to test these strings hashsums. After testing that string, I need the next string, "112". Then, "113", "121", "122", and so on. When the string reaches "333" and the string is not the one I'm looking for, it was obviously no string with only 3 characters (all 3-character-combinations possible with the array have been tried). So I need to start again using 4 characters. That´s why "333" is supposed to become "1111". 
Hope this helps.
EDIT #2:
I found the error. I redimensioned my array incorrectly, so the last index was empty. This made my strings look weird. Thank you all for your working solutions, have a good day!