OK so i just started to learn vb and did some experimenting until i came across this thing which i cant understand
So i have a console application with two modules
Module1.vb (program starts form this)
Module Module1
Sub Main()
add.addone()
add.addone()
add.addone()
Console.Read()
End Sub
End Module
add.vb
Public Module add
Private counter As Integer
Public Sub addone()
counter += 1
Console.Write(counter)
End Sub
End Module
I expect it to print on the console a pattern of:
1
1
1
Why does it print out
1
2
3
Im asking this because, what i understand is, after add.addone() is called. The variable (counter) should no longer exist! So when add.addone() is called for the second time it should be blank and thus, printing out 1 again.
I hope you can help me. What am i doing wrong and why is this happening?
thanks,
Vidhu
Why would that be?
counteris declared outside ofaddone, in the surrounding moduleadd. So of course it remains in existence even after the method exits.(Incidentally, you should follow the .NET PascalCase naming convention for modules and methods …)