Results 1 to 10 of 10

Thread: variables and loops...

  1. #1

    Thread Starter
    Hyperactive Member gmatteson's Avatar
    Join Date
    Feb 2002
    Location
    Rhode Island, USA
    Posts
    293

    variables and loops...

    if i have a loop and an if...then statement inside that loop with a variable being dimmed inside the if...then, will that waste memory?

  2. #2
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    If this is similar to what you are trying to do , then Yes , it's waste of memory . Here , in below example , the variable 'aa' will be dimmed 5 times if the first condition was met otherwise nothing happen .

    VB Code:
    1. Dim i As Integer
    2.         Dim a As String = "a", b As String = "b"
    3.  
    4.         For i = 0 To 5
    5.             If a = "a" Then
    6.                 Dim aa As String = "a"
    7.                 'do other stuff
    8.             ElseIf b = "b" Then
    9.                 'do something
    10.             End If
    11.  
    12.         Next

  3. #3

    Thread Starter
    Hyperactive Member gmatteson's Avatar
    Join Date
    Feb 2002
    Location
    Rhode Island, USA
    Posts
    293

    differnet in vb.net?

    i thought it was different in .net where the variable was destroyed once it's container finished, for..loop, if..then, procedure... guess not. thanks though

  4. #4
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Actual gmatteson you are correct the variable scope is only valid inside the If statement. Although it will be initialized 5 times or with each loop, but it will also be destroyed in that loop. So if you only need it for something inside that if..then then that is the proper way to do it.

  5. #5
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Hmm , still wasting memory .

  6. #6

    Thread Starter
    Hyperactive Member gmatteson's Avatar
    Join Date
    Feb 2002
    Location
    Rhode Island, USA
    Posts
    293

    cool


  7. #7
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Well it doesn't waste memory because it only creates the the variable or takes the memory if the IF statement is true and then it uses it and then destroys it. It is most efficent since no memory is used if not needed and instead of 5 there is one 1 at a time. If you created it outside of the then it would be used regardless of whether or not it was needed.

  8. #8
    Fanatic Member VBCrazyCoder's Avatar
    Join Date
    Apr 2003
    Posts
    681
    But say the GC doesn't collect the objects until after the looping has completed, then you would still have the memory allocated until that point. Or at least, the GC has more work to do.

    Hope I don't start another GC debate

  9. #9
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by VBCrazyCoder
    But say the GC doesn't collect the objects until after the looping has completed, then you would still have the memory allocated until that point. Or at least, the GC has more work to do.
    Hope I don't start another GC debate
    And say loop is long , still wasting memory in my view point . This doesn't mean you guys are wrong but as I know , it's bad practice to dim variables within loops and IFs with some exceptions of course .

    [i]
    Hope I don't start another GC debate [/B]
    lol.......

  10. #10
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106
    Wouldn't a locally declared variable like that be created on the stack? If that is the case, there is no waste of memory, since the memory is already allocated (this might be true even without the stack), and the object goes away when the function or the loop exits.

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