tips
[Top] [All Lists]

REALbasic Tip: The power of the Dim statement

To: REALbasic-tips at lists dot realsoftware dot com
Subject: REALbasic Tip: The power of the Dim statement
From: Geoff Perlman <geoff at realsoftware dot com>
Date: Tue, 13 Jul 2004 11:27:39 -0500
Cc:
Delivered-to: REALbasic-tips at lists dot realsoftware dot com
List-help: <mailto:realbasic-tips-request@lists.realsoftware.com?subject=help>
List-id: REALbasic Tips <realbasic-tips.lists.realsoftware.com>
List-post: <mailto:realbasic-tips@lists.realsoftware.com>
You can't use REALbasic without understanding how the Dim statement works. But what you might not know is that it's much more flexible in REALbasic 5.5.

For example, you can assign default values to variables:

Dim x As Integer = 5

You can also assign the same default value to several variables:

Dim x, y, z As Integer = 5

You can use functions to provide the default value:

Dim x As Integer = Ticks

You can create new instances of objects:

Dim d As New Date

Be careful however when declaring multiple variables on the same line. For example:

Dim today, tomorrow As New Date

creates two variables that both point to the same object. So changing the values of the Today variable would automatically change Tomorrow as well since they are both the same object. If you want separate objects, use two Dim statements:

Dim today As New Date
Dim tomorrow As New Date

You can also put these on the same line if you'd like:

Dim today As New Date, tomorrow As New Date

And don't forget that starting with REALbasic 5, you no longer have to put your Dim statements at the top of a method. You can place them anywhere as long as they are not inside a block construct like an If statement or a loop. This is handy because it allows you to put the Dim statement near where you begin using the variable. If it turns out that you don't need the variable after all, it's easier to remember to delete the Dim statement if it's near where it's used.
--
Geoff Perlman
President and CEO
REAL Software, Inc.

<Prev in Thread] Current Thread [Next in Thread>
  • REALbasic Tip: The power of the Dim statement, Geoff Perlman <=