Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Steve's Quick Lesson on DIM
#1
Let's talk about DIM for a moment!  To help us have this conversation, let's refer to the following code, which you can compile and stare at in your IDE if you want, just to test and make certain that the syntax is, indeed, correct.

Code: (Select All)
Dim a, b, c
Dim d As Integer, e As Double, f As Single
Dim g, h, i As _Float
Dim As String j, k, l


Now, let's break down what we're doing here, line by line.

Our first line of code is:
Code: (Select All)
Dim a, b, c

This is the simplest type of DIM statement, and it simply says that we're going to officially DIM and name three variables to whatever the default type is.  Without any DEF or _DEFINE statement, these three variables are all going to be the default type of SINGLE.

Code: (Select All)
Dim d As Integer, e As Double, f As Single

Here, we're assigning a set type of each variable in that single DIM statement.  d is an integer.  e is a double.  f is single.  Easy enough to follow along with the logic behind this statement, right?

Code: (Select All)
Dim g, h, i As _Float
Now this line gets a little tricky for some people, as they confuse it with the newest syntax for DIM.  They think that the way it's written, all the variables on this line are _FLOAT.  They're not!  Just compare this line to the one above it, and then apply the same logic as the first DIM statement we looked at.  What types are the three variables here?

g is implicitly defined to be our default variable type -- SINGLE, in this case.  So is h.  i, on the other hand, is explicitly defined by the user to be a _Float.

Code: (Select All)
Dim As String j, k, l
 And here, we have the new syntax for how one can DIM variables.  If you notice, the type comes first on the left, with all the variables of this explicit type to be to the right of the type definition.  j, k, and l are all String type variables.

See the difference in the 3rd and 4th lines' syntax, and understand why they're two completely different things?

If you want to define a large number of variables as a single type all at once, use the DIM AS <TYPE> syntax.  Anything else would be incorrect.  Wink
Reply
#2
Great explanation Steve. I still had worries about this being the correct way for the multiple DIMing of vars, but now I get it. Thanks
Reply
#3
Wiki page updated back to where this information is now in it properly. https://qb64phoenix.com/qb64wiki/index.php/DIM Big Grin
Reply




Users browsing this thread: 1 Guest(s)