Declaring a Variable

To declare a variable in STATISTICA Visual Basic use the keyword Dim as the following demonstrates:

'Creates a string variable
Word As String

'Creates a double-precision variable
Number As Double

The declaration of the variable must take place prior to using it; otherwise a redefinition error will occur.  The following illustrates this problem:

'Because you didn't declare "Number",
'SVB implicitly creates it for you

Number = 5.3

'SVB created "Number" for you already,
'so you can't declare the same variable twice.
'This won't work

Number As Double

If a variable is not explicitly declared by you then the STATISTICA Visual Basic interpreter will internally create it for you of the type variant. This may be undesirable for some users because the variant type consumes more memory than other types. Also, it may be difficult to manage variables when they are not explicitly defined by you. the following demonstrates this:

Sub Main

'SVB creates a local variable called "Word"
'for you because you didn't declare it

Word = "Hello"


'The message box will say "Hello", not "Hello, World"

End Sub


Sub OtherFunction

'SVB creates another local variable called "Word"
'for you because you didn't declare it.
'Note that it is not using the "Word" from the
'Main function

Word = "Hello, World"

End Sub

The problem here is that it may appear that the procedure OtherFunction is referencing the variable Word from the Main function; however, this is not the case. SVB generates a variant variable called Word for both functions because neither of them were explicitly declared.

To force yourself to declare variables before using them in you programs use the expression Option Explicit in the declaration section. The following example illustrates this:

Option Explicit

Sub Main

'This won't work;
'it must be declared by you

Number = 5

End Sub