|
-
Jul 7th, 2004, 01:20 PM
#1
Thread Starter
Hyperactive Member
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
-
Jul 7th, 2004, 02:51 PM
#2
Member
Johnson, Tom G
Johnson Tom G
Johnson Tom
VB Code:
' (wholename is johnson, tom g)
Function MN(ByVal WholeName As String) As String
Dim index As Integer
Dim temp As String
index = wholename.IndexOf(" ")
temp = wholename.Substring(index, wholename.Length - index)
' MsgBox(temp)
' index = "Tom G".IndexOf(" ")
' MsgBox(index)
index = temp.IndexOf(" ")
If index <> 0 Then
MN = temp.Substring(index, wholename.Length - index)
Else
MN = ""
End If
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:
Function MiddleName(ByVal WholeName As String) As String
Dim arr() As String
Dim MName as String
wholename = Replace(wholename, ",", "")
arr = Split(wholename, " ")
Try
MName = " " + arr(2) + " "
Catch ex As Exception
MName = ""
End Try
return MName
End Function
-
Jul 7th, 2004, 11:23 PM
#3
Can't we do a UBound check?
VB Code:
Function MiddleName(ByVal WholeName As String) As String
Dim arr() As String
Dim MName As String
wholename = Replace(wholename, ",", "")
arr = Split(wholename, " ")
'MessageBox.Show(UBound(arr).ToString)
If UBound(arr) = 2 Then
MName = " " + arr(1) + " "
Else
MName = ""
End If
Return MName
End Function
Or am I following bad practice here?
-
Jul 8th, 2004, 11:12 AM
#4
Lively Member
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
-
Jul 8th, 2004, 11:57 AM
#5
Member
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:
Function MiddleName(ByVal WholeName As String) As Array
Dim ReturnArray(3) As String
Dim arr() As String
Dim MName As String
arr = Split(Replace(WholeName, ",", ""), " ")
Select Case arr.Length
Case 1
ReturnArray(0) = arr(0)
ReturnArray(1) = ""
ReturnArray(2) = ""
Case 2
ReturnArray(0) = arr(0)
ReturnArray(1) = arr(1)
ReturnArray(2) = ""
Case 3
ReturnArray(0) = arr(0)
ReturnArray(1) = arr(1)
ReturnArray(2) = arr(2)
Case Else
ReturnArray(0) = ""
ReturnArray(1) = ""
ReturnArray(2) = ""
End Select
Return ReturnArray
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|