tags:

views:

214

answers:

2

How might one retrieve the last element of a string array in visual basic 6?

I'm dealing with filenames with multiple dots which are split into an array, and I want to manipulate only the extension. The following code works, but has a hardcoded element I want to remove.

Private Sub Form_Load()
    Dim aPath() As String
    Dim FileName As String
    Dim realExt As String

    FileName = "A long dotty.file.name.txt"
    aPath = Split(FileName, ".")

    realExt = aPath(3) ' <-- how to not hardcode?'

    MsgBox ("The real extension is: " & realExt)
    Unload Me
End Sub
+2  A: 
realExt = aPath(ubound(aPath))
shahkalpesh
-- thank you much!
matt wilkie
+8  A: 

I think using Ubound should do the trick :

Private Sub Form_Load()
    Dim aPath() As String
    Dim FileName As String
    Dim realExt As String

    FileName = "A long dotty.file.name.txt"
    aPath = Split(FileName, ".")

    realExt = aPath(UBound(aPath))

    MsgBox ("The real extension is: " & realExt)
    Unload Me
End Sub
MaxiWheat