chicago75 Posted November 28, 2002 Posted November 28, 2002 Help.. I am using the String.format() method and getting the following error... An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll Additional information: Input string was not in a correct format. Here is the line of the code causing it... strResult = String.Format("{^\d-\d{3}-\d{3}-\d{4}}", hpformato) The "hpformato" is Dimmed as String. I don't know why this is not working..Help Chicago75@evildata.com Quote
*Gurus* Derek Stone Posted November 28, 2002 *Gurus* Posted November 28, 2002 (edited) It's expecting a numeric data type.strResult = String.Format("{###-###-####}", Decimal.Parse(hpformato)) Edited November 28, 2002 by Derek Stone Quote Posting Guidelines
wyrd Posted November 28, 2002 Posted November 28, 2002 String.Format doesn't work with regular expressions. It has it's own set of rules (ie; # for an integer). Also you need to specify the location of the value you are formatting... example; Dim phoneNum As Integer = 2092524321 'Note that Console.WriteLine follows the same format rules as String.Format Console.WriteLine("Phone number is {0:(###) ###-####}", phoneNum) To plop an object inside a sentence you use {n} (n stands for the nth place). If you want to format it you place the format rules after a colon :. Does that makes sense? In respect it's similar to using.. Console.WriteLine("Some {0} to {1}", wordZero, wordOne) except you're adding formatting. Does this make sense? Quote Gamer extraordinaire. Programmer wannabe.
chicago75 Posted November 28, 2002 Author Posted November 28, 2002 Thanks.... However I am still getting the same error..... I have attached the updated code to help make sense of this.... Please let me know what I am doing wrong... Public Function PhoneFormat(ByVal hpformat As String) As String Dim strResult As String Dim iLength As Integer Dim strOriginal As String Dim i As Integer strOriginal = hpformat hpformat = Replace(hpformat, ")", "") hpformat = Replace(hpformat, "(", "") hpformat = Replace(hpformat, "-", "") hpformat = Replace(hpformat, ".", "") hpformat = Replace(hpformat, Space(1), "") iLength = Len(hpformat) For i = 1 To iLength Next i Select Case iLength Case Is = 10 hpformat = strResult Case Is = 7 strResult = String.Format("{###-####}", Decimal.Parse(hpformat)) GoTo Exit_Proc Case Else strResult = "1" GoTo Exit_Proc End Select strResult = String.Format("{###-###-####}", Decimal.Parse(hpformat)) Exit_Proc: PhoneFormat = strResult hpformat = PhoneFormat End Function Quote
*Gurus* Derek Stone Posted November 28, 2002 *Gurus* Posted November 28, 2002 That's an error on my part, which wyrd had correct. strResult = String.Format("{[b]0:[/b]###-###-####}", Decimal.Parse(hpformat)) Quote Posting Guidelines
wyrd Posted November 28, 2002 Posted November 28, 2002 Derek: Any specific reason why you're using Decimal.Parse? String.Format parameters in n position accept objects, it will accept anything he passes to it. Quote Gamer extraordinaire. Programmer wannabe.
chicago75 Posted November 28, 2002 Author Posted November 28, 2002 Derek/Wyrd Thanks...It works KUDOS!!!!! Quote
*Gurus* Derek Stone Posted November 28, 2002 *Gurus* Posted November 28, 2002 Any specific reason why you're using Decimal.Parse?I like to be explicit. Who knows what garbage you'd get if you input something non-numeric. Quote Posting Guidelines
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.