Page 1 of 2 12 LastLast
Results 1 to 40 of 49

Thread: Declaration Style Opinion

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2000
    Location
    Nitro
    Posts
    633
    Hi everyone,

    I want to ask for your opinion. Below there are two sub procedures. Both does the samething. Only difference is the declaration of the variables. Does it matter if I declare a variable on the fly or is it more appropriate to declare all variables at the beginning?



    Method1
    Code:
    Sub Main()
      Dim str_Data As String
      str_Data = "Method1"
      MsgBox str_Data
    
      Dim int_X As Integer
      int_X = 1
      MsgBox int_X
    End Sub
    Method2
    Code:
    Sub Main()
      Dim str_Data As String
      Dim int_X As Integer
      
      str_Data = "Method2"
      MsgBox str_Data
      
      int_X = 1
      MsgBox int_X
    End Sub
    Chemically Formulated As:
    Dr. Nitro

  2. #2
    Fanatic Member
    Join Date
    Feb 2000
    Location
    The Netherlands
    Posts
    715
    I think it doesn't matter. Before compiling, vb automatically moves the lines to the beginning.
    Oetje
    [email protected]
    93606776
    Visual Basic 6, Windows 2000

    Never pet a burning dog

  3. #3
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    a matter or preference:

    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  4. #4
    Fanatic Member
    Join Date
    Jan 2000
    Location
    Mobile, AL, USA
    Posts
    600

    Post

    From what I've read, it's standard programming procedure (whatever that means ) to declare all variables at the top of procedures.

    All the best.

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2000
    Location
    Nitro
    Posts
    633

    Smile

    Thank you Oetje, HeSaidJoe and OneSource. I have a very unorthodox way of programming and was just concern about the performance of the program.

    Thank you again.
    Chemically Formulated As:
    Dr. Nitro

  6. #6
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    Nope, it doesn't matter where you declare it.
    But it's a good idea to declare them at the top 'cause otherwise you'll get confused wheter you declared it or not.
    But like HeSaidJoe said, it's just a matter of preference
    But I also like declaring strings and integers like this

    Code:
    Dim MyStr$  'Instead of ... As String
    Dim MyInt%  'Instead of ... As Integer
    'I think it looks neet, simple and professional :)
    Jop - validweb.nl

    Alcohol doesn't solve any problems, but then again, neither does milk.

  7. #7
    Fanatic Member
    Join Date
    Jan 2000
    Location
    Mobile, AL, USA
    Posts
    600

    Thumbs up Thanks Jop.

    I didn't know that variables could be dimmed like that.

  8. #8
    Hyperactive Member gravyboy's Avatar
    Join Date
    Jan 2000
    Location
    Where I was before . . . if you don't know then you're new!
    Posts
    334

    Exclamation Godammn!

    Have you ever tried to work your way through code that is written in an unorthodox way? Couple that with converting legacy VB3 apps to VB6 and the headache potential is huge . . .
    Matt G
    VS6 Ent SP5 @ Work
    VS6 Ent SP5 & VB.Net @ Home
    [email protected]



  9. #9
    Guest

    Re: Thanks Jop.

    Originally posted by OneSource
    I didn't know that variables could be dimmed like that.
    I didn't know that variables could be declared like that.

  10. #10
    Fanatic Member
    Join Date
    Jan 2000
    Location
    Mobile, AL, USA
    Posts
    600

    Megatron...

    Originally posted by Megatron
    Originally posted by OneSource
    I didn't know that variables could be dimmed like that.
    I didn't know that variables could be declared like that.
    What's the big deal? Just a little VB slang!

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2000
    Location
    Nitro
    Posts
    633
    Your style does the job Jop but I am use to Hungarian Notation. I think most people at my company use your style of notation.

    Thanks everyone!
    Chemically Formulated As:
    Dr. Nitro

  12. #12
    Fanatic Member HaxSoft's Avatar
    Join Date
    May 2000
    Location
    Ohio
    Posts
    593

    A kind of ReDim?

    Hey Nitro, your question made me wonder about the following situation:

    Code:
    Private Sub Form_Load()
    
        Dim a As Integer
        
        For a = 1 To 10
            Dim b As Long
            
            b = b + 1
        Next a
        
        Debug.Print b
        
    End Sub
    Does anyone know? Does b get dimmed once or ten times in this code?

    Just wondering.

  13. #13
    Hyperactive Member tumblingdown's Avatar
    Join Date
    Mar 2000
    Posts
    362
    As we all now, vb's garbage collection is not the best when it comes to fairly complicated objects. This is why you should not declare objects 'on the fly' in vb. Declare them in the morning, and clean them up last thing before you go to bed.


    Here's a question:

    What happens to an objects passed byVal to a function,
    when the function loses scope? i.e.

    public sub WhereDidMyMemoryGo(Byval oAnyOldObject as object)
    '/ some stuff
    end sub


    You could make this question harder by asking what if the function is in another apartment, thread, or process (who's doing then marshalling;-) ??



    td.
    "One logical slip and an entire scientific edifice comes tumbling down." - Robert M. Pirsig


    [email protected]

    "but if Einstein is right and God is in the details, reality requires that we sometimes get religion." - Scott Meyers.

  14. #14
    Hyperactive Member gravyboy's Avatar
    Join Date
    Jan 2000
    Location
    Where I was before . . . if you don't know then you're new!
    Posts
    334

    10X Loop

    Hax,

    it gets declared 10 times. VB may well manage the memory for this and just reuse the same space, but get into habits like this and the world may stop . . .

    Matt G
    VS6 Ent SP5 @ Work
    VS6 Ent SP5 & VB.Net @ Home
    [email protected]



  15. #15
    Fanatic Member
    Join Date
    Oct 2000
    Location
    London
    Posts
    1,008

    Thumbs up Sloppy

    If you want some really sloppy code, don't declare them at all and let VB hand you a big fat Variant...

    Jop,

    only problem with Dim int%, str$ is readability - if you do not know the post fixes it can be confusing - As String is unambiguous.

    As a matter of style I always declare my intentions at the start...

    Cheers,

    Paul.
    Not nearly so tired now...

    Haven't been around much so be gentle...

  16. #16
    Guest
    From a style and maintenance point of view, you should declare you variables at the beginning of the procedure with meaningful names, preferably including some form of hungarian notation. Don't use the postfix declaration, cos it is a dog to maintain. Don't call your variables x or a - use something decent.

    The only exception for that is if you need to declare an object variable that is memory hungry, or takes while to initialise, and is only used in certain conditions:

    eg:

    Code:
    Private Sub Process()
        Dim lstrText as String
        Dim llngLoopCount as Long
        Dim lobjItem as SomeObject
    
        'Do some stuff
    
        For llngLoopCount = 1 to 100
            lstrText = "XXX"
            lobjItem = GetItem(llngCount, lstrText)
            If lobjItem.Err = 12345 then
                'This is a very rare case (ie. the error is rare)
                'and needs a special handling mechanism
                Dim lobjSpecialObject as SomeLargeObject
                lobjSpecialObject.HandleError
                Set lobjSpecialObject = Nothing
            End If
        Next llngCount
    
    End Sub
    The above example ONLY declares the SomeLargeObject, which takes a long time to initialise (for whatever reason) if the condition where it is used is required. This means that the process time for the loop when an error doens't happen is reduced, and the memory the SomeLargeObject requires is not used unless absolutely required.

    Having said that, I would only recommend using that conditional declaration approach in special cases, and only for large memory or init-time intensive object. I wouldn't use it for any sort of standard data type - they are fast to create and use very little memory.

    Even if you are only coding by yourself and not in a team, it's good practice to make sure you code conforms to some standards, cos when you do end up working in a team environment, your life will be that much easier, and you code very easy to maintain, which makes everybody happy. There is nothing worse than having to maintain spaghetti code. I know, I've been there many times.

    - gaffa

    (I hate editing for typos)


    [Edited by gaffa on 11-03-2000 at 06:03 AM]

  17. #17
    Hyperactive Member gravyboy's Avatar
    Join Date
    Jan 2000
    Location
    Where I was before . . . if you don't know then you're new!
    Posts
    334

    Ermm...

    Gaffa,

    I was under the impression that VB compiled then ran and so would read all the variable declarations independently of whether they are in an If..Then..Else statement. Consequently the memory would still be in use.

    I am willing to be corrected - for as MicroSoft have proved recently 'nobody is infallible'
    Matt G
    VS6 Ent SP5 @ Work
    VS6 Ent SP5 & VB.Net @ Home
    [email protected]



  18. #18
    Guest
    To be honest gravy boy, you could be right... I'm not actually as sure now that I think about it. I do seem to recall reading about this technique somewhere, but it is entirely possible I'm wrong. I jsut spoke to another guy I work with, and he's is also under the impression that conditional declarations work. But.......who knows.

    Probably worth some further research. I very, very rarely use the technique though...

    - gaffa

  19. #19
    Hyperactive Member tumblingdown's Avatar
    Join Date
    Mar 2000
    Posts
    362
    Technically it does not matter where you declare a variable.
    It is not initialised until the first time it is used.

    If you declare a variable in a loop, you are still only declaring it once.




    td.
    "One logical slip and an entire scientific edifice comes tumbling down." - Robert M. Pirsig


    [email protected]

    "but if Einstein is right and God is in the details, reality requires that we sometimes get religion." - Scott Meyers.

  20. #20
    Hyperactive Member gravyboy's Avatar
    Join Date
    Jan 2000
    Location
    Where I was before . . . if you don't know then you're new!
    Posts
    334

    OK

    At'll do me . . .
    Matt G
    VS6 Ent SP5 @ Work
    VS6 Ent SP5 & VB.Net @ Home
    [email protected]



  21. #21
    Fanatic Member
    Join Date
    Oct 2000
    Location
    London
    Posts
    1,008

    Variable lifetimes...

    Hi td,

    You are partially correct, but - it depends where the variables are - if they are declared in a BAS module, for instance, then memory will be allocated and the variables initialised to their default values on the Application starting up. They will retain memory until the application exits even if they are never used!. Variables in Forms and Class modules share the life of that form or class. Only Dynamic local variables (i.e. declared within a procedure) will not exist until the procedure is run. A Dim will allocate memory and set the value as default - the variable does not have to be used to take up memory.

    Cheers,

    Paul.
    Not nearly so tired now...

    Haven't been around much so be gentle...

  22. #22
    Hyperactive Member tumblingdown's Avatar
    Join Date
    Mar 2000
    Posts
    362
    Paul,

    I know, i was only pointing out about the about the location in a looooooooop.


    I take it your a city lad, how's Fortis.



    td.
    "One logical slip and an entire scientific edifice comes tumbling down." - Robert M. Pirsig


    [email protected]

    "but if Einstein is right and God is in the details, reality requires that we sometimes get religion." - Scott Meyers.

  23. #23
    Fanatic Member
    Join Date
    Oct 2000
    Location
    London
    Posts
    1,008
    Hi td,

    You could say Fortis is tumblingdown

    Where are you?

    It is not initialised until the first time it is used.
    that made me believe you were not aware of the memory implications of Dim and its default values etc.

    Quite right about the loop.

    Sorry for dissin' you.

    Cheers,

    Paul.
    Not nearly so tired now...

    Haven't been around much so be gentle...

  24. #24
    Hyperactive Member tumblingdown's Avatar
    Join Date
    Mar 2000
    Posts
    362
    Well i suppose technically i'm at CSFB, but i think most of the time i'm just off in my own little world...

    Oh, and just got back after a few bevvies at lunch. Best time of the day, that post liquid lunch feeling ;-)



    td.


    "One logical slip and an entire scientific edifice comes tumbling down." - Robert M. Pirsig


    [email protected]

    "but if Einstein is right and God is in the details, reality requires that we sometimes get religion." - Scott Meyers.

  25. #25
    Hyperactive Member gravyboy's Avatar
    Join Date
    Jan 2000
    Location
    Where I was before . . . if you don't know then you're new!
    Posts
    334
    Wayhay! Same as meself . . . .


    I often find that if you go out drinking with the management more gets done in terms of man-management . . .
    Matt G
    VS6 Ent SP5 @ Work
    VS6 Ent SP5 & VB.Net @ Home
    [email protected]



  26. #26
    Fanatic Member
    Join Date
    Oct 2000
    Location
    London
    Posts
    1,008
    OK gravyboy and tumblingdown - we obviously cannot pass up the chance to have a career enhancing beer (or seven). However I am assuming that you are down in Canary Wharf - could be tricky.

    Send me a mail with some contact details if you fancy some liquid refreshment.

    mail me at [email protected]

    P.S. Sorry for hijacking your thread Nitro

    Cheers (or should that be Bottoms Up!)

    Paul.
    Not nearly so tired now...

    Haven't been around much so be gentle...

  27. #27
    Hyperactive Member gravyboy's Avatar
    Join Date
    Jan 2000
    Location
    Where I was before . . . if you don't know then you're new!
    Posts
    334
    Oh, and just got back after a few bevvies at lunch. Best time of the day, that post liquid lunch feeling ;-)
    Unfortunately I have just left the Marble Arch in Manchester and may be a litte late for the beer . . .
    Matt G
    VS6 Ent SP5 @ Work
    VS6 Ent SP5 & VB.Net @ Home
    [email protected]



  28. #28
    Fanatic Member
    Join Date
    Oct 2000
    Location
    London
    Posts
    1,008

    Spooky...

    gravyboy,

    according to your profile you are with Centrica (I had assumed that when you said 'same as meself', you were talking about CSFB not the beer (how stupid can I be....),

    however, getting back to the point I used to work for BG before it split into a million pieces...

    VB World; British Gas; Beer spooky coincidences, or what...






    What (hehehe)

    and Yeh I have had a few beers meself...

    Cheers,

    Paul.
    Not nearly so tired now...

    Haven't been around much so be gentle...

  29. #29
    Hyperactive Member tumblingdown's Avatar
    Join Date
    Mar 2000
    Posts
    362
    Paul, mail sent.

    td,
    "One logical slip and an entire scientific edifice comes tumbling down." - Robert M. Pirsig


    [email protected]

    "but if Einstein is right and God is in the details, reality requires that we sometimes get religion." - Scott Meyers.

  30. #30
    Hyperactive Member gravyboy's Avatar
    Join Date
    Jan 2000
    Location
    Where I was before . . . if you don't know then you're new!
    Posts
    334
    There's another BG bod around here Gary.Lowe . . . ex BG anyway!
    Matt G
    VS6 Ent SP5 @ Work
    VS6 Ent SP5 & VB.Net @ Home
    [email protected]



  31. #31
    Hyperactive Member tumblingdown's Avatar
    Join Date
    Mar 2000
    Posts
    362
    btw, now beating you by 2 posts hehe.

    td
    "One logical slip and an entire scientific edifice comes tumbling down." - Robert M. Pirsig


    [email protected]

    "but if Einstein is right and God is in the details, reality requires that we sometimes get religion." - Scott Meyers.

  32. #32
    Fanatic Member
    Join Date
    Oct 2000
    Location
    London
    Posts
    1,008

    Posting, posting....

    td - Excellent news...
    Not nearly so tired now...

    Haven't been around much so be gentle...

  33. #33
    Fanatic Member
    Join Date
    Oct 2000
    Location
    London
    Posts
    1,008
    but I think...
    Not nearly so tired now...

    Haven't been around much so be gentle...

  34. #34
    Fanatic Member
    Join Date
    Oct 2000
    Location
    London
    Posts
    1,008
    I have posted...
    Not nearly so tired now...

    Haven't been around much so be gentle...

  35. #35
    Fanatic Member
    Join Date
    Oct 2000
    Location
    London
    Posts
    1,008
    one more than you. HEHEHEHEHE

    Cheers,

    Paul.
    Not nearly so tired now...

    Haven't been around much so be gentle...

  36. #36
    Hyperactive Member tumblingdown's Avatar
    Join Date
    Mar 2000
    Posts
    362
    ****, beating me.






    oh no, your not afterall. would you look at that.


    td.
    "One logical slip and an entire scientific edifice comes tumbling down." - Robert M. Pirsig


    [email protected]

    "but if Einstein is right and God is in the details, reality requires that we sometimes get religion." - Scott Meyers.

  37. #37
    Fanatic Member
    Join Date
    Jan 2000
    Location
    Mobile, AL, USA
    Posts
    600

    Sorry to interrupt guys

    But I'm just removing the automatic "email notification" option for me for this post.

  38. #38
    Hyperactive Member tumblingdown's Avatar
    Join Date
    Mar 2000
    Posts
    362
    sorry mate, we'll stop.

    Oi! Paul, pack it in! i said No!


    td.
    "One logical slip and an entire scientific edifice comes tumbling down." - Robert M. Pirsig


    [email protected]

    "but if Einstein is right and God is in the details, reality requires that we sometimes get religion." - Scott Meyers.

  39. #39
    Fanatic Member
    Join Date
    Oct 2000
    Location
    London
    Posts
    1,008
    Not me guv.

    I was over in Chit-Chat wondering about CHICKEN.

    BTW td - you are sick (can't wait for the beer)

    Paul

    P.S. Sorry everybody, we'll stop now.
    Not nearly so tired now...

    Haven't been around much so be gentle...

  40. #40
    Junior Member
    Join Date
    Oct 2000
    Location
    Purgatory
    Posts
    31
    Just some more automatic data type definition characters
    I'm not sure if it actually speeds up your project.
    Code:
    dim something$ 'string
    dim something% 'integer
    dim something& 'long
    dim something! 'single
    dim something# 'double
    Paul Bousa

Page 1 of 2 12 LastLast

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