genespos
genespos

Reputation: 3311

Is there any issue/difference using literal data types

Is there any difference between:

Dim Str As String
Dim Int As Integer
Dim Lng As Long
Dim Sng As Single
Dim Dbl As Double
Dim Dec As Decimal

and

Dim Str$
Dim Int%
Dim Lng&
Dim Sng!
Dim Dbl#
Dim Dec@

Can I use abbreviations without problems?

Upvotes: 1

Views: 65

Answers (3)

Matt Wilko
Matt Wilko

Reputation: 27342

There is no difference whatsoever. See this page for the details of these.

These are a carryover from previous versions of Visual Basic and have (rightly) fallen out of general use, so you should try and avoid using these.

Also - it can get confusing (maybe not to you, but definitely to other people looking at your code) because all of those characters have other meanings:

  • $ String Interpolation character in VS2015
  • & String concatenation
  • ! Field shortcut operator
  • # Code Region

And For anyone familiar with C#:

  • % Modulus operator in C#
  • @ String literal in C#

Note: As long as you have Option Infer On (and always make sure you have Option Strict On) - In a lot of cases you don't need to define the type, because this is correctly inferred:

Dim a = CalculateSum(1, 2)

In this case CalculateSum returns an Integer, so a is declared as an Integer.

Upvotes: 2

Sascha
Sascha

Reputation: 1218

Don't do that. Those abbreviations are relics from the past. They have been around since the time of Basic. It hurts the readability of your code. Because there is IntelliSense you don't lose time typing the non-abbreviated code. If you really want to abbreviate, let the compiler pick the type like this:

dim str = "my string"

(for example).

For this to work, the Option Infer must be active

Upvotes: 3

chrisl08
chrisl08

Reputation: 1658

There is no difference. https://msdn.microsoft.com/en-us/library/s9cz43ek.aspx

The only problem is code readability and maintainability. Another programmer who is not familiar with the type suffixes will not be able to easily maintain your code.

Upvotes: 1

Related Questions