Defining Variables

1 post / 0 new
Brian Element's picture
Brian Element
Offline
Joined: 07/11/2012 - 19:57
Defining Variables

In IDEA if you use the option explicit it will force you to define a variable by using the Dim statement.  If you don't define a variable, all the variables are defined as variants.  Also IDEA allows you to place several variables on the same line such as:

Dim myString1, myString2, myString3, myString4 as string

Now I had always assumed that all 4 of these variables would be defined as string variables but actually only the last variable, myString4 is defined as string and the other 3 would be defined as empty.  In most cases you would never notice a problem unless you are sending a variable to a function such as:

Sub Main

Dim myString1, myString2, myString3, myString4 as string

call myFunction(myString1)

end sub

function myFunction(byRef thisString as string)

...

end function

If you tried this out you would get an error because you are sending a variable which you thought is a string but actually isn't to a function that is expecting a string.  To get around this you should define each variable on its own line, this will get around this problem.  So best practice is to declare each variable on its own line.