|
-
Jan 6th, 2007, 10:22 AM
#1
Thread Starter
Addicted Member
[RESOLVED] Variable Declaration
I know that in VB6
1)
VB Code:
'Incorrect declaration
Dim var1,var2,var3 as string
'var1,var2 will be variant only var3 will be treat as string
'Correct declaration
Dim var1 as String
Dim var2 as String
Dim var3 as String
My question is could we declare like this
VB Code:
Dim var1 as String, var2 as String, var3 as String
I often declare like that cause i'm too lazy to write Dim.
2) In other langugages, variables should be declare at the beginning of the procedure.
However, in VB6 variables can be declare anywhere in the procedure. Is there any inconvernient to this? ( memory loss... or something.)
Like
VB Code:
Dim var1 as String
Dim var2 as String
' Code Here
' Or
Dim var1 as String
' Code Here, We don't declare var2 here since we don't need it at the moment
Dim var2 as String
'Code, now we need it so we declare
-
Jan 6th, 2007, 10:27 AM
#2
Re: Variable Declaration
I find it very confusing if variables are declared not-in-the-top, because that is where you can get a glimpse of all variables that are being used. It makes tracking of variables easier atleast when you're unsure if a variable is global or local. Keep them at top.
As for writing only one Dim and having multiple variables declared there, I see no problem with that. I actually dislike high vertical variabled declaration lists that have only one variable declared per line Although I do (or try to) categorize related variables together.
There are common naming conventions for variables for an additional topic: strExample, lngTestValue, curTotal, dtmYesterday...
-
Jan 6th, 2007, 10:28 AM
#3
Re: Variable Declaration
1. That is fine.
2. My understanding is you can declare them anywhere, as the compiler allocates (variable) memory, prior to the actual executuion. (However, I always put them at the top of the Sub/Fuction/Form for readability)
-
Jan 6th, 2007, 11:34 AM
#4
Thread Starter
Addicted Member
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|