TypeName... VbTypeName... What tha?

Update: all appears to have returned to normal on Gallifrey so, here's the link to Paul's original post: http://www.panopticoncentral.net/PermaLink.aspx/4b3ebfc7-e421-4c98-b37a-ab2969946cf0

Unfortunately, as I write this entry the Panopticoncentral feed is down - and I don't have a copy of the post that I'm about to mention - but, today Paul Vick wrote about the unusually named parameter of the VbTypeName function - urtName; so, the signature for this method is:

    VbTypeName( ByVal urtName As String ) As String

Paul was basically writing about how urtName was a not-obvious name and then explained how "urt" stands for "Unique Resource Typename" (or something) - i.e.: System.String, System.Int16, etc. So, basically, you pass in a Type name and receive back a string that is the VB equivalent name for that Type:

    Response.Write( VbTypeName( "System.String" ) )    ' Displays String

An interesting aside from all of this is the actual name of the function: VbTypeName! I mean, we don't have any of the following...

    VbCStr( ... )
    VbMid( ... )
    VbInStr( ... )

...do we? So, why on Earth would they choose this Hungarian'ish sounding name for a function?

After I read Paul's entry I immediately contacted Bill to get the "low down" on this quirky language aspect. After a few e-mails, Bill was able to enlighten me as to the reasons for this rather anomalous sounding noun.

The reason for VbTypeName is because of the other VB function named TypeName! My initial reaction was, "so... what about overloads - haven't you guys heard of that?". Well, as it turned out, apparently they have heard of overloads but this was a case where 2 different functions were required because the 2 functions are semantically different and do not consistently differ between returns and arguments. This is what happens if I pass an Integer to both functions:

    Dim foo As Integer = 1
    Response.Write(VbTypeName(foo))    ' Displays Nothing (not "Nothing")
    Response.Write(TypeName(foo))      ' Displays Integer

...and, this is what happens if I pass strings:

    Dim foo As String = "foo"
    Response.Write(VbTypeName(foo))  ' Displays Nothing (not "Nothing")
    Response.Write(TypeName(foo))    ' Displays String

If the VB Team had implemented a single function with the following signatures:

    Overloads Public TypeName( ByVal name As Object ) As String 
    Overloads Public TypeName( ByVal name As String ) As String

..and I passed an Integer, they would both return expected values however, when passing strings I could never call the TypeName function that takes an object when passing a String instance as the argument. So, this is clearly a case where the semantics are completely different and 2 functions are definitely needed.

For the record, here's some examples of returns from both functions. Do I ever plan to use VbTypeName ------ nup!

    Response.Write("String: " & VbTypeName("System.String") & "<--")      ' Displays String
    Response.Write("Int16: " & VbTypeName("System.Int16") & "<--")        ' Displays Short
    Response.Write("Byte: " & VbTypeName("System.Byte") & "<--")          ' Displays Byte
    Response.Write("Decimal: " & VbTypeName("System.Decimal") & "<--")    ' Displays Decimal
    Response.Write("Conn: " & VbTypeName("System.Data.SqlClient.SqlConnection") & "<--")    ' Displays Nothing (not "Nothing")
    Response.Write(TypeName(New System.Data.SqlClient.SqlConnection))     ' Displays SqlConnection 
    
Dim foo As String = "Bar" Response.Write(VbTypeName(foo)) ' Displays Nothing (not "Nothing") Response.Write(TypeName(foo)) ' Displays String

No Comments