0

i want to change first letter of both First name and Last Name to uppercase here is my code however, i am getting error..as

Index and length must refer to a location within the string.
Parameter name: length

MY CODE IS AS BELOW :

Function uppercasefirstletter(ByVal oldString As String) As String
Dim firstletter As Char
Dim newString As String
firstletter = Char.ToUpper(oldString.Substring(0, 1))
Mid(oldString, 1, 1) = firstletter
newString = oldString
Return newString

End Function

PLZ LET ME KNOW THE ERROR AS QUICK AS POSSIBLE...AM WORKING WITH VB.NET 2005 THIS CODE HOWEVER WORKED IN VB.NET 2003 PLZ HELP ME TO SOLVE THE ERROR
THANKS
CYA
ROHAN

2
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by laghaterohan
0

You are calling function with an empty string which causes that error in oldString.Substring(0, 1) You should check for an empty string first:

Function uppercasefirstletter(ByVal oldString As String) As String
'
If String.IsNullOrEmpty(oldString) Then
  Return oldString
Else
  Return oldString.Substring(0, 1).ToUpper & oldString.Substring(1, oldString.Length - 1)
End If

End Function
0

thanks hp it works..ll mark it solved if it works......thanks in advance once again.. :)

cya
Rohan

You are calling function with an empty string which causes that error in oldString.Substring(0, 1) You should check for an empty string first:

Function uppercasefirstletter(ByVal oldString As String) As String
'
If String.IsNullOrEmpty(oldString) Then
  Return oldString
Else
  Return oldString.Substring(0, 1).ToUpper & oldString.Substring(1, oldString.Length - 1)
End If

End Function
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.