Question

Just out of curiosity:

I know I can tell the compiler if I want a value to be interpreted as a certain numeric type, e.g. as Integer (32 bit signed), this way appending an "I" (type character) to the constant value:

Private Function GetTheAnswerAsInteger() As Integer

   Return 42I

End Function

There's also "S" for Short, "D" for Decimal, etc.

But what is the suffix for Byte? Hint: it's not the obvious one "B"...

Was it helpful?

Solution

There isn't one. If you need to distinguish between an integer and a byte (e.g. to call an appropriate overload) for a constant, you need to cast.

(The same is true in C#, by the way.)

MSDN provides confirmation:

Byte has no literal type character or identifier type character.

There's also a list of type characters and literal suffixes.

OTHER TIPS

So, we added binary literals in VB last fall and got similar feedback from early testers. We did decide to add a suffix for byte for VB. We settled on SB (for signed byte) and UB (for unsigned byte). The reason it's not just B and SB is two-fold.

One, the B suffix is ambiguous if you're writing in hexadecimal (what does 0xFFB mean?) and even if we had a solution for that, or another character than 'B' ('Y' was considered, F# uses this) no one could remember whether the default was signed or unsigned - .NET bytes are unsigned by default so it would make sense to pick B and SB but all the other suffixes are signed by default so it would be consistent with other type suffixes to pick B and UB. In the end we went for unambiguous SB and UB. -- Anthony D. Green,

https://roslyn.codeplex.com/discussions/542111

It has been integrated to the upcoming VB.NET release, and this is the way it will work:

Public Const MyByte As Byte = 4UB;
Public Const MyByte2 As SByte = 4SB;

This answer does not really provide a suffix, but it's as close as it gets.

If you define an extension method as

Imports System.Runtime.CompilerServices

Module IntegerExtensions

    <Extension()> _
    Public Function B(ByVal iNumber As Integer) As Byte
        Return Convert.ToByte(iNumber)
    End Function

End Module

You can use it like this:

Private Function GetTheAnswerAsByte() As Byte

   Return 42.B

End Function

There's no byte literal in .NET.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top