Results 1 to 5 of 5

Thread: Name function...help!!!

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2004
    Location
    Utah, USA
    Posts
    353

    Name function...help!!!

    This is the data I can have:

    Johnson, Tom G
    Johnson Tom G
    Johnson Tom

    This is the function I have so far:

    Function MiddleName(ByVal WholeName As String) As String
    Dim arr() As String
    Dim MName As String
    Dim pos As Integer

    WholeName = Replace(WholeName, ",", " ")
    arr = Split(WholeName, " ")
    pos = WholeName.IndexOf(" ")

    If pos > 0 Then
    MiddleName = " " + arr(2) + " "
    Else
    MiddleName = ""
    End If
    Return MiddleName

    End Function

    It gets the middle name, but if there is no middle name it gives me an error. How can I fix that? Please help!

    Brenda

  2. #2
    Member
    Join Date
    Jun 2004
    Location
    The Netherlands
    Posts
    37
    Johnson, Tom G
    Johnson Tom G
    Johnson Tom

    VB Code:
    1. ' (wholename is johnson, tom g)
    2. Function MN(ByVal WholeName As String) As String
    3.  
    4.    Dim index As Integer
    5.    Dim temp As String
    6.  
    7.    index = wholename.IndexOf(" ")
    8.    temp = wholename.Substring(index, wholename.Length - index)
    9.  
    10.    ' MsgBox(temp)
    11.    ' index = "Tom G".IndexOf(" ")
    12.    ' MsgBox(index)
    13.  
    14.    index = temp.IndexOf(" ")
    15.  
    16.    If index <> 0 Then
    17.        MN = temp.Substring(index, wholename.Length - index)
    18.    Else
    19.         MN = ""
    20.    End If
    21. End Function


    Now why this doesn't work is beyond me (first tried it in a while loop, then rewrote to check)...

    It doesn't work because; index = temp.indexof(" ") returns 0, while it should return 3! If you remove this line and put back in the lines which are now commented out it shows that;
    temp = "Tom G"
    index = 3

    Baffles me....

    Although I don't like it, this does work;

    VB Code:
    1. Function MiddleName(ByVal WholeName As String) As String
    2.  
    3.    Dim arr() As String
    4.    Dim MName as String
    5.  
    6.    wholename = Replace(wholename, ",", "")
    7.    arr = Split(wholename, " ")
    8.  
    9.    Try
    10.        MName = " " + arr(2) + " "
    11.    Catch ex As Exception
    12.        MName = ""
    13.    End Try
    14.    
    15.    return MName
    16.  
    17. End Function

  3. #3
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Can't we do a UBound check?

    VB Code:
    1. Function MiddleName(ByVal WholeName As String) As String
    2.  
    3.         Dim arr() As String
    4.         Dim MName As String
    5.  
    6.         wholename = Replace(wholename, ",", "")
    7.         arr = Split(wholename, " ")
    8.  
    9.         'MessageBox.Show(UBound(arr).ToString)
    10.         If UBound(arr) = 2 Then
    11.             MName = " " + arr(1) + " "
    12.         Else
    13.             MName = ""
    14.         End If
    15.  
    16.  
    17.        
    18.         Return MName
    19.  
    20.     End Function

    Or am I following bad practice here?

  4. #4
    Lively Member
    Join Date
    Jun 2004
    Posts
    74
    I agree with mendhak, check the upper bound of your arr variable. If it is 1 (then you have two names only, and no middle name). If it is >1, then the middle name will be in index 2 (arr(2)) as you have in your code.

    The reason you get an error is that you are asking for in index that doesn't exist when there is no middle initial/name.

    Hume

  5. #5
    Member
    Join Date
    Jun 2004
    Location
    The Netherlands
    Posts
    37
    Didn't like the try/catch myself, but couldn't solve it before, this should work, although calling this function with;

    dim temp(3) as string
    temp = MiddleName("Johnson, Tom G")

    Gives a error with option strict on, I don't get that cuz as far as I can see I assign an array to an array, it works nonetheless.

    VB Code:
    1. Function MiddleName(ByVal WholeName As String) As Array
    2.         Dim ReturnArray(3) As String
    3.         Dim arr() As String
    4.         Dim MName As String
    5.  
    6.  
    7.         arr = Split(Replace(WholeName, ",", ""), " ")
    8.         Select Case arr.Length
    9.             Case 1
    10.                 ReturnArray(0) = arr(0)
    11.                 ReturnArray(1) = ""
    12.                 ReturnArray(2) = ""
    13.             Case 2
    14.                 ReturnArray(0) = arr(0)
    15.                 ReturnArray(1) = arr(1)
    16.                 ReturnArray(2) = ""
    17.             Case 3
    18.                 ReturnArray(0) = arr(0)
    19.                 ReturnArray(1) = arr(1)
    20.                 ReturnArray(2) = arr(2)
    21.             Case Else
    22.                 ReturnArray(0) = ""
    23.                 ReturnArray(1) = ""
    24.                 ReturnArray(2) = ""
    25.         End Select
    26.  
    27.         Return ReturnArray
    28. End Function

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width