Unsigned Types


As you
may have seen on the roadmap ( href="http://msdn.microsoft.com/vstudio/productinfo/roadmap.aspx">http://msdn.microsoft.com/vstudio/productinfo/roadmap.aspx),
we’ve been working on several new language features for VB .NET. style='mso-spacerun:yes'>  
One of them includes unsigned types. style='mso-spacerun:yes'>  Yes, VB is finally getting unsigned types,
and this will make some tasks a lot easier.

 

Specifically,
we are adding intrinsic support for four new integral types, three of them
unsigned, one of them signed.  Here’s a
table which describes the new types:

 

style='border-collapse:collapse;mso-yfti-tbllook:480;mso-padding-alt:0in 5.4pt 0in 5.4pt'>

style='mso-bidi-font-weight:normal'>VB Keyword

style='mso-bidi-font-weight:normal'>Conversion Operator

style='mso-bidi-font-weight:normal'>Type Character

style='mso-bidi-font-weight:normal'>CLR Name

style='font-size:10.0pt;font-family:Verdana'>SByte style='font-size:10.0pt;font-family:Verdana'>

style='font-size:10.0pt;font-family:Verdana'>CSByte style='font-size:10.0pt;font-family:Verdana'>(o)

style='font-size:10.0pt;font-family:Verdana'>–none–

style='font-size:10.0pt;font-family:Verdana'>System.SByte style='font-size:10.0pt;font-family:Verdana'>

style='font-size:10.0pt;font-family:Verdana'>UShort style='font-size:10.0pt;font-family:Verdana'>

style='font-size:10.0pt;font-family:Verdana'>CUShort style='font-size:10.0pt;font-family:Verdana'>(o)

style='font-size:10.0pt;font-family:Verdana'>US

style='font-size:10.0pt;font-family:Verdana'>System.UInt16

style='font-size:10.0pt;font-family:Verdana'>UInteger style='font-size:10.0pt;font-family:Verdana'>

style='font-size:10.0pt;font-family:Verdana'>CUInt style='font-size:10.0pt;font-family:Verdana'>(o)

style='font-size:10.0pt;font-family:Verdana'>UI

style='font-size:10.0pt;font-family:Verdana'>System.UInt32

style='font-size:10.0pt;font-family:Verdana'>ULong style='font-size:10.0pt;font-family:Verdana'>

style='font-size:10.0pt;font-family:Verdana'>CULng style='font-size:10.0pt;font-family:Verdana'>(o)

style='font-size:10.0pt;font-family:Verdana'>UL

style='font-size:10.0pt;font-family:Verdana'>System.UInt64

 

For
consistency with earlier versions of VB, the name of the original type makes up
the root of the new name, prefixed by a U (or S) to denote the class=SpellE>signededness
of the type.  We used a similar approach to name the conversion operators and type
characters.   style='mso-bidi-font-weight:normal'>SByte has no type character
because Byte has no type character.

 

Here’s a
code snippet which uses each of these new types:

 

Module style='font-size:10.0pt;font-family:"Lucida Console";mso-bidi-font-family:"Courier New"'>
Module1

     style='color:blue'>Sub Main class=GramE>()

       
Dim a As
SByte = -127

       
Dim b style='color:blue'>As
UShort
= 65535

       
Dim c style='color:blue'>As
UInteger
= 4294967295

       
Dim d style='color:blue'>As
ULong
= 18446744073709551615UL

 

       
Dim o style='color:blue'>As
Object

 

       
o = CSByte class=GramE>(
42)

       
a = CSByte class=GramE>(
o)

 

       
o = 42US

       
b = CUShort class=GramE>(
o)

 

       
o = 42UI

       
c = CUInt class=GramE>(
o)

 

       
o = 42UL

       
c = CULng class=GramE>(
o)

     style='color:blue'>End Sub

End style='font-size:10.0pt;font-family:"Lucida Console";mso-bidi-font-family:"Courier New"'>
Module

Of
course, this snippet is a contrived example demonstrating the new syntax. style='mso-spacerun:yes'>  
Here’s a real-world example:

 

Module style='font-size:10.0pt;font-family:"Lucida Console";mso-bidi-font-family:"Courier New"'>
ExternalFunctions

     style='color:blue'>Structure INFO

       
Public KIND style='color:blue'>As
UShort style='color:blue'>

       
Public FLAGS style='color:blue'>As
UShort style='color:blue'>

       
Public DATA style='color:blue'>As
UInteger style='color:blue'>

     style='color:blue'>End Structure

 

     style='color:blue'>Declare Auto style='color:blue'>Sub GetData style='color:blue'>Lib "mylib" ( class=SpellE>ByRef Info class=GramE>As INFO)

     style='color:blue'>Declare Auto style='color:blue'>Function GetCount style='color:blue'>Lib "mylib" _

style='mso-spacerun:yes'>        ( class=SpellE>ByVal Index class=GramE>As class=SpellE>UInteger) As class=SpellE>UInteger style='color:blue'>

 

     style='color:blue'>Sub Enumerate()

       
For i style='color:blue'>As
UInteger
= 0 To GetCount class=GramE>(10)

           
Dim Info style='color:blue'>As
INFO

           
GetData class=GramE>(
Info)

           
Console.WriteLine class=GramE>(
Info.KIND)

           
Console.WriteLine class=GramE>(
Info.DATA)

       
Next

     style='color:blue'>End Sub

End style='font-size:10.0pt;font-family:"Lucida Console";mso-bidi-font-family:"Courier New"'>
Module

 

One final
note:  unsigned types also affect method
overload resolution.  If two methods are
equally applicable because the argument types do not exactly match the
signature, yet the methods differ only by the signededness
of a parameter, the method with the signed parameter wins (except for class=SpellE>SByte
and style='mso-bidi-font-weight:normal'>Byte, where Byte wins).  This rule
reduces the occurrence of ambiguity errors.

 

Comments (7)

  1. Anonymous says:

    What work is being done with unsigned values and bit operations (and, or, <<, >>)? Today, we have the issue of the signed bit causing problems when using the signed values and trying to do bit manipulation on values over a certain ceiling. Unsigned values should solve this issue (as it does in languages that already have unsigned values). I just want to hear from someone that all will be as expected on this front.

  2. Anonymous says:

    Correct, unsigned types will solve this problem. And, Or, <<, and >> will behave with unsigned types as they do in other languages.

  3. Anonymous says:

    At a very low priority, could you look at puting back both Eqv and Imp? Their use fulness as logical operators is not that high (= & >= will do, though an "Implies" operator that short-circuits on the first argument being False would be good), they’re still rather useful bit manipulations.
    (BTW I know Eqv == Not(XOr) and x Imp y == (Not x) Or y.)
    Alternatively, why did VB ever have these ‘esoteric’ operators?

  4. Anonymous says:

    When will this be incorporated into the Studio.Net?