I get the feeling Jonathan may not realize what he's saying, nor does he
realize what he's not seeing.
Jonathan's example clearly shows the fundamentals of global vs. local
variables, a concept which has been a part of VB since it's early days.
However, the code in question and it's assessment (mentioned by Lee Moody) I
believe to be accurate.
The text below is from the VB.NET online help, Visual Basic Language
Concept, under the heading Levels of Scope (it should also answer Mike
Lerch's question about lifetime):
======================================
Block Scope
A block is a set of statements terminated by an End, Else, Loop, or Next
statement, for example within a For...Next or If...Then...Else...End If
construction. An element declared within a block can be used only within
that block. In the following example, the scope of the integer variable Cube
is the block between If and End If, and Cube can no longer be referenced
when execution passes out of the block:
If N < 1291 Then
Dim Cube As Integer
Cube = N ^ 3
End If
Note Even if the scope of an element is limited to a block, its lifetime
is still that of the entire procedure. If you enter the block more than once
during the procedure, a block variable retains its previous value. To avoid
unexpected results in such a case, it is wise to initialize the variable
each time.
======================================
Regards,
Mark
Quote:
> > Then why can I not access an object outside of the block
> > that it created within?
> You can, you just need a reference to it. Example...
> Public MyCar as Car
> Sub CreateCar()
> Dim temp as Car
> temp = New Car
> temp.Name = "something"
> MyCar = Car
> End Sub
> Sub Test
> Messagebox (MyCar.Name)
> End Sub
> The object is created in CreateCar subroutine. But that same object can be
> accessed in the Test subroutine.
> In this example, 'MyCar' and 'temp' are Reference Variables. They are not
> the objects themselves, but rather a pointer to the object.
> --
> Jonathan Allen, MCSD
> > Then why can I not access an object outside of the block
> > that it created within? Variables are no
> > longer 'variables' they are in fact objects themselves are
> > they not?
> > >-----Original Message-----
> > >> The objects dimensioned only exist once the code is
> > >> executed, and only exist within the block that they are
> > >> created.
> > >That's not quite accurate. Local variables may only exist
> > within the block
> > >they are created, but objects exist on the heap and thus
> > have indefinite
> > >lifespans.
> > >--
> > >Jonathan Allen, MCSD
> > >> The objects dimensioned only exist once the code is
> > >> executed, and only exist within the block that they are
> > >> created.
> > >> For example, the 'intCounter' in the second dim
> > statement
> > >> below only exists within the if/then block that it's
> > >> created within. If you try to access 'intCounter' after
> > >> the 'end if' then you will get an error (assuming you
> > >> have 'option explicit on' set).
> > >> It's a nice way to only create variables/objects when
> > you
> > >> need them and automatically discard them when you're
> > done.
> > >.