Results 1 to 4 of 4

Thread: [RESOLVED] Variable Declaration

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Dec 2005
    Posts
    230

    Resolved [RESOLVED] Variable Declaration

    I know that in VB6
    1)
    VB Code:
    1. 'Incorrect declaration
    2. Dim var1,var2,var3 as string
    3. 'var1,var2 will be variant only var3 will be treat as string
    4.  
    5. 'Correct declaration
    6. Dim var1 as String
    7. Dim var2 as String
    8. Dim var3 as String

    My question is could we declare like this
    VB Code:
    1. 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:
    1. Dim var1 as String
    2. Dim var2 as String
    3. ' Code Here
    4.  
    5.  
    6. ' Or
    7. Dim var1 as String
    8. ' Code Here, We don't declare var2 here since we don't need it at the moment
    9.  
    10. Dim var2 as String
    11. 'Code, now we need it so we declare

  2. #2
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    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...

  3. #3
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    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)

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Dec 2005
    Posts
    230

    Re: Variable Declaration

    thanks guys

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width