Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

Posted

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?

Gamer extraordinaire. Programmer wannabe.
Posted

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

Posted

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.

Gamer extraordinaire. Programmer wannabe.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...