appending I and % to Integers

  • Thread starter Thread starter John A Grandy
  • Start date Start date
J

John A Grandy

could someone explain the following to me :

Appending the literal type character I to a literal forces it to the Integer
data type. Appending the identifier type character % to any identifier
forces it to Integer.
 
This is what they mean:

Dim val1 As Integer = 5 'Normal
Dim val2 As Integer = 5% 'With %
Dim val3 As Integer = 5I 'With I

Note that the above sample is not useful because 5 is an interger by
default. But, for example, if you want to make sure that 5 is a single then
you could use this:

Dim val4 As Single = 5.0!

Hope that helps.
Lance
 
John,
Appending the literal type character I to a literal forces it to the
Integer
data type.
If you don't give a type on a numeric literal it defaults to Integer, you
can use a type character to ensure that it is specifically that type.

For example try the following, watching "value" in the Auto Watch window:

Dim value As Object

' Integer (32-bit integer) literals
value = 1
value = 2I
value = 3%

' Short (16-bit integer) literals
value = 1S

' Long (64-bit integer) literals
value = 1L
value = 2&

' Decimal literals
value = 1D
value = 2@

' Single literals
value = 1.0F
value = 2.0!

' Double literals
value = 1.0R
value = 2.0#

I normally favor the alpha character (I, S, L, F, R, D) over the special
character (%, &, !, #, @). The special characters are a hold over from
original Basic (pre Visual Basic).

The one I find missing is Byte literals.

Appending the identifier type character % to any identifier
forces it to Integer.
Allows both of the follow to work with Option Strict On. I never use the
type characters on identifiers (the second line).

Dim x As Integer
Dim y%

As you can see you can use either I or % as a suffix on a numeric literal,
however you cannot use the I suffix on an identifier, as there is no way to
determine if the suffix is a type character or part of the identifier.

Hope this helps
Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top