I try to describe my problem step by step because I do not know how to say it in correct programming terms.
When I use a System.String type, I do the following:
- Declare the type:
Dim Str1 as String - Assign its value:
Str1 = "This is a string"
I want to create a new type that just like the System.String type but in different name. For example, I want to create a UrlString type for string like this:
- Declare the type:
Dim Str2 as UrlString - Assign its value:
Str2 = "http://www.example.com"
My question is: How do I create the UrlString type?
The reason: I want to create the UrlString type to help me to identify the value of the content. For example, UrlString type means the string is in url format, PhoneString means the string is in phone format, CreditCardString type means the string is in credit card format and so on.
UPDATE:
Thanks Marc Gravell and GSerg. Here is the solution:
Class UrlString
Private ReadOnly value As String
Public Sub New(ByVal value As String)
Me.value = value
End Sub
Public Shared Widening Operator CType(ByVal value As String) As UrlString
Return New UrlString(value)
End Operator
Public Shared Widening Operator CType(ByVal u As UrlString) As String
Return u.value
End Operator
Public Overrides Function GetHashCode() As Integer
Return If(value Is Nothing, 0, value.GetHashCode())
End Function
Public Overrides Function Equals(ByVal obj As Object) As Boolean
Return String.Equals(value, DirectCast(obj, String))
End Function
Public Overrides Function ToString() As String
Return value
End Function
End Class
You need to add an implicit conversion operator from
stringtoUrlStringfor that to work. In C#:Then: