Today, I came across a code example like the following:
Option Explicit
Sub MySub()
Dim iCount As Integer
For iCount = 0 To 100
Debug.Print "Arbitrary Code Here"
If iCount Mod 2 = 0 Then
Dim myString As String 'This is the line I'm curious about
myString = iCount
Debug.Print myString
End If
Next iCount
End Sub
Once a variable goes in scope in VB6 and/or VBA, it stays in scope throughout the procedure.
So, is it a bad programming practice to declare a variable in a loop? Is it better to declare myString either at the top of the procedure or, at the very least, above the FOR LOOP?
UPDATE: There are some good answers here. I hoped my question was objective, but there's enough personal opinion here to, IMHO, make it subjective.
I'm marking Jeff's answer as the "right" one; mainly because I agree with what he says. If I were to answer my own question, it would be 'yes' and for the same reason as Jeff's.