cjard, would it make you happier if I named it PhoneNumber instead of Phone?
Probably more like "PhoneNumberString"
Having this phone "type" makes a lot of since to me. All of our phone database fields are consistently char(10). This type will have a parse function as well as format functions. It will accept string or long input. For example, the following code is all valid:
Dim phoneA As USPhoneNumber = "6315551212"
Dim phoneB As USPhoneNumber = 6315551212
Dim phoneC As USPhoneNumber = "(631) 555-1212"
Dim phoneD As USPhoneNumber = "1-631-555-1212"
OK, so we can say that your phone number is analogous to Microsoft's DateTime, because there may be many different formats, and (as JohnH mentioned) a phone number has several components.. Maybe an area code, non-geographical identifer, country code, extension. A date have many components, hours, minutes, seconds etc..
What I'm saying is, Microsoft didnt provide a CType operator on date; you have to Parse or ParseExact it..
Accordingly, if I were making a class to handle phone numbers, I would follow a similar pattern, and my PhoneNumber would have a Parse (and maybe a ParseExact if I was really looking to drive myself mental)
In my code, you wouldnt see any of these:
Dim phoneA As USPhoneNumber = "6315551212"
Dim phoneB As USPhoneNumber = 6315551212
Dim phoneC As USPhoneNumber = "(631) 555-1212"
Dim phoneD As USPhoneNumber = "1-631-555-1212"
You'd have:
Dim phoneA As USPhoneNumber = USPhoneNumber.Parse("6315551212")
Dim phoneB As USPhoneNumber = USPhoneNumber.Parse(6315551212)
Dim phoneC As USPhoneNumber = USPhoneNumber.Parse("(631) 555-1212")
Dim phoneD As USPhoneNumber = USPhoneNumber.Parse("1-631-555-1212")
Of course, in the end, Im sure the same methods get called, but it's about how the code documents itself. I'm an Option Strict = On programmer. I dont expect to see unrelated object types being assigned to each other.
I'm not saying what you want to do is wrong; it just makes me go "Ugh" because (IMHO) it looks sloppy/like you dont know or care what Types are
If I did:
Dim phoneE As String = "hello"
that would be perfectly valid. So, instead of having validation and a seperate function to display the phone in the (###) ###-#### format, I now have the phone number strongly typed. When I put phoneA.ToString() into the database, I know I have a valid phone number. And when I use phoneA.ToFormatedPhoneNumber() I can have it return "(631) 555-1212"
This makes perfect sense to me, and indeed, it's not the bit I dont think is good; your argument for representing something as complex as a phone number in its own type is highly commendable.. Its just realising it by direct assignment from a String that looks wrong..
I wasnt overjoyed by VB6's default properties for a similar reason, and I guess Microsoft follow that idea too, because default properties can now only be found on things that embody some kind of collection.
You now can no longer say:
Textbox1 = "Hello World"
You must say:
Textbox1.Text = "Hello world"
However:
Dim myStrings as New List<string>
myStrings(0) = "hello"
myStrings.Item(1) = "Hello"
are equivalent.
This looks okay; because they are collections and the syntax is familiarly sensible from array..