Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the suffix (type character) for "Byte" numeric constants in VB.NET?

Tags:

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"...

like image 630
splattne Avatar asked Oct 31 '08 07:10

splattne


People also ask

What are numeric constant in Visual Basic?

The constants refer to fixed values that the program may not alter during its execution. These fixed values are also called literals. Constants can be of any of the basic data types like an integer constant, a floating constant, a character constant, or a string literal. There are also enumeration constants as well.

What is literals in VB net?

A literal is a value that is expressed as itself rather than as a variable's value or the result of an expression, such as the number 3 or the string "Hello".


2 Answers

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.

like image 101
Jon Skeet Avatar answered Sep 23 '22 04:09

Jon Skeet


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; 
like image 35
Erti-Chris Eelmaa Avatar answered Sep 23 '22 04:09

Erti-Chris Eelmaa