views:

42

answers:

1

Is it possible to format what I have below to return a phone number?

private string _Phone;
    [DataMember]
    public string Value
    {
        get { return String.Format("{0:(###) ###-####}", Int64.Parse(_Phone)); }
        set { _Phone = value; }
    }
+1  A: 

You could use String.Format if you convert the string to a long first.

String.Format("{0:(###) ###-####}", Int64.Parse("8005551212"))
DarkBobG
@DarkBobG -- so how would i modify what i posted?
hersh
Just return the value from the String.Format above.I would test first whether or not the string is null or whitespace before doing it, and if it is, pass back your default value above.
DarkBobG
@DarkBobG -- is what I have correct? If not, would you mind showing me how this is done?
hersh
That should do what you want. Like I said, just make sure that you have a value that will parse properly. Otherwise you'll end up with a weird string coming out of that format. You could test the String.Length and anything less than 10 would return a default of some sort, etc... But you've got the general idea.
DarkBobG
@DarkBobG -- thanks so much!
hersh