VBAHole22 Posted December 1, 2004 Posted December 1, 2004 Maybe I'm having a brain fart from too much turkey but answer me this. Look at the following code: Dim i As Integer For i = 0 To 30 Dim stuff, huh As String stuff = "43524525" huh = "3452345" Next Now if I put a break on the 'stuff=' line and run it; the first time around stuff will be equal to nothing. But what about the second time around? Quote Wanna-Be C# Superstar
Mike_R Posted December 1, 2004 Posted December 1, 2004 The best way to know is to test it! But the answer is that the Scope of your 'stuff' variable is local to that block, but its Lifetime will persist until the Sub or Function is exited. That is, it will only be 'Dimmed' but once, even if it loops multiple times. So the 2nd time through your loop, the line: Dim stuff, huh As String will be ignored for it already has been Dimentioned, that is, allocated space on the Stack, etc. So the 2nd time through, you'll find that the value of 'stuff' will already = "43524525". You should test this yourself though to see. More interesting might be to use an Integer variable in the same manner, incrementing it each time through the loop. You'll notice that the variable is sort of "Static", in that it's value from the previous time is maintained. It will be maintained until the Sub or Function istelf is exited. (But it is not *truly* static, for a Static var maintains its value between calls to the Sub or Function.) Also note that by dimming within the block, this value is not accessible outside that block. That is, the variable's Scope is limited to within this block. Quote Posting Guidelines Avatar by Lebb
VBAHole22 Posted December 1, 2004 Author Posted December 1, 2004 Those are precisely the results I observed. It is the same with int or string. I'm shocked. You think you know someone, you let them in your home, and then they do this to you! Can anyone confirm if this behavior existed in VB6? Quote Wanna-Be C# Superstar
Mike_R Posted December 1, 2004 Posted December 1, 2004 It's the *same* within VB6, however VB6 has sloppier Scoping rules. For example:Private Sub Form_Load() Dim i As Integer For i = 0 To 30 Dim stuff, huh As String stuff = "43524525" huh = "3452345" Next MsgBox stuff 'Returns "3452345" End Sub Note that the final MsgBox returns the value for 'stuff' even though stuff is declared within a block. VB6 does not care about where a variable is declared. VB.Net is tighter with the scoping rules; witness the following:Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load Dim i As Integer For i = 0 To 30 Dim stuff, huh As String stuff = "43524525" huh = "3452345" Next MessageBox.Show (stuff) ' <-- CTE: "Name 'stuff' is not declared." End Sub Try copying the above into your VB.Net and it won't even run. You'll get a Compile-Time Error stating "Name 'stuff' is not declared." But the internals within the loop are basically the same. Quote Posting Guidelines Avatar by Lebb
donnacha Posted December 3, 2004 Posted December 3, 2004 The golden rule in .Net is never assume anythong and always init your variables to a default value..... Quote Hamlet
VBAHole22 Posted December 3, 2004 Author Posted December 3, 2004 and in this case resest each variable for every turn in the loop Dim i As Integer For i = 0 To 30 Dim stuff, huh As String stuff= string.empty huh = string.empty stuff = "43524525" huh = "3452345" Next Quote Wanna-Be C# Superstar
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.