Scope misunderstanding

E

edamron

I'm going through a web cast series and have discovered that I have a
profound lack of understanding when it comes to the scope of object
created in the Load Sub of forms. The instructor creates many objects
in the Load event handler. However isn't it true that when the Load
sub exits these object will be destroyed?? I don't see how what he is
doing can possibly work. Below is the Load handler. Please notice
that although he creates the objects he doesn't do anything with them
so what's the point? Thanks in advance.

Private Sub FormMain_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Me.controller = New UIControl()

'**
'** hookup handlers for events raised by controller
'**
AddHandler controller.ExitTheApp, AddressOf Me.FormMain_Exit
AddHandler controller.ShowSalaryUI, AddressOf Me.ShowSalaryUI
AddHandler controller.HideSalaryUI, AddressOf Me.HideSalaryUI
AddHandler controller.EmptyTextField, AddressOf Me.InvalidName
AddHandler controller.ClearEmployeeInfo, AddressOf
Me.ClearEmployeeInfo
AddHandler controller.UpdateEmployeeInfo, AddressOf
Me.UpdateContactInfoTabPage
AddHandler controller.UpdateEmployeeInfo, AddressOf
Me.UpdateHomeAddrTabPage

'** File >> Show/Hide:
Dim toggleMenuAdapter As ToggleMenuItemAdapter
toggleMenuAdapter = New
ToggleMenuItemAdapter(Me.ShowSalaryToolStripMenuItem, _
New SimpleCommand(AddressOf controller.ShowSalaryTabPage),
_
New SimpleCommand(AddressOf controller.HideSalaryTabPage))

'** File >> Exit:
Dim menuAdapter As MenuItemAdapter
menuAdapter = New MenuItemAdapter(Me.ExitToolStripMenuItem, _
New SimpleCommand(AddressOf controller.ExitApp))

'** cmdLookup Click:
Dim lookupAdapter As LookupAdapter
lookupAdapter = New LookupAdapter(Me.cmdLookup, _
me.txtFirstName, me.txtLastName, New
LookupCommand(AddressOf controller.Lookup))

'** TextBox Validating:
Dim textboxValidatingAdapter As TextBoxValidatingAdapter
Dim validatingCmd As ValidatingCommand
validatingCmd = New ValidatingCommand(AddressOf
controller.NonEmpty_Validating)
textboxValidatingAdapter = New
TextBoxValidatingAdapter(Me.txtFirstName, validatingCmd)
textboxValidatingAdapter = New
TextBoxValidatingAdapter(Me.txtLastName, validatingCmd)
End Sub
 
M

Mattias Sjögren

However isn't it true that when the Load
sub exits these object will be destroyed??

No. The lifetime of an object doesn't directly depend on the scope of
a particular variable holding a reference to the object. An object
will be destroyed eventually when there are no more references to it.


Mattias
 
M

Michael C

edamron said:
I'm going through a web cast series and have discovered that I have a
profound lack of understanding when it comes to the scope of object
created in the Load Sub of forms. The instructor creates many objects
in the Load event handler. However isn't it true that when the Load
sub exits these object will be destroyed?? I don't see how what he is
doing can possibly work. Below is the Load handler. Please notice
that although he creates the objects he doesn't do anything with them
so what's the point? Thanks in advance.

I think what you don't understand (if I understand correctly) is that it is
important where the *variable* is that is pointing to the object, not where
the object is created, eg:

in this case x is defined at module level so will have the lifetime of the
form:

Dim x as MyObject

Private Sub FormMain_Load(...) Handles MyBase.Load
set x = new MyObject
End sub

but in this case x is defined in form load so will go out of scope at the
end of form load. Note where the object is created is not relevant, it is
where the object is dimmed that is important.

Private Sub FormMain_Load(...) Handles MyBase.Load
Dim x as MyObject
set x = new MyObject
End sub

to get even more fancy you could have 2 references to the *same* object, so
only 1 object exists but 2 variables reference it.

dim y as MyObject

Private Sub FormMain_Load(...) Handles MyBase.Load
Dim x as MyObject
set x = new MyObject
set y = x
End sub
 
C

Chris Dunaway

Michael said:
Private Sub FormMain_Load(...) Handles MyBase.Load
Dim x as MyObject
set x = new MyObject
set y = x
End sub

Are you channeling the spirit of a VB6 guru?
 
E

edamron

Michael said:
I think what you don't understand (if I understand correctly) is that it is
important where the *variable* is that is pointing to the object, not where
the object is created, eg:

in this case x is defined at module level so will have the lifetime of the
form:

Dim x as MyObject

Private Sub FormMain_Load(...) Handles MyBase.Load
set x = new MyObject
End sub

but in this case x is defined in form load so will go out of scope at the
end of form load. Note where the object is created is not relevant, it is
where the object is dimmed that is important.

Private Sub FormMain_Load(...) Handles MyBase.Load
Dim x as MyObject
set x = new MyObject
End sub

to get even more fancy you could have 2 references to the *same* object, so
only 1 object exists but 2 variables reference it.

dim y as MyObject

Private Sub FormMain_Load(...) Handles MyBase.Load
Dim x as MyObject
set x = new MyObject
set y = x
End sub

Thanks for the reply Michael.

It seems to me that he IS Dim the objects in the load event handler
isn't he? So when that event finishes and exits wouldn't all of the
objects he Dim'd be eligible for garbage collection?
 
E

edamron

Michael said:
I think what you don't understand (if I understand correctly) is that it is
important where the *variable* is that is pointing to the object, not where
the object is created, eg:

in this case x is defined at module level so will have the lifetime of the
form:

Dim x as MyObject

Private Sub FormMain_Load(...) Handles MyBase.Load
set x = new MyObject
End sub

but in this case x is defined in form load so will go out of scope at the
end of form load. Note where the object is created is not relevant, it is
where the object is dimmed that is important.

Private Sub FormMain_Load(...) Handles MyBase.Load
Dim x as MyObject
set x = new MyObject
End sub

to get even more fancy you could have 2 references to the *same* object, so
only 1 object exists but 2 variables reference it.

dim y as MyObject

Private Sub FormMain_Load(...) Handles MyBase.Load
Dim x as MyObject
set x = new MyObject
set y = x
End sub

Thanks for the reply Michael.

It seems to me that he IS Dim the objects in the load event handler
isn't he? So when that event finishes and exits wouldn't all of the
objects he Dim'd be eligible for garbage collection?
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

edamron said:
It seems to me that he IS Dim the objects in the load event handler
isn't he? So when that event finishes and exits wouldn't all of the
objects he Dim'd be eligible for garbage collection?

Not if there is a reference to the object stored somewhere else.

When you declare a reference in a method, it's only the reference that
belongs to the scope, any object that you create in the method is not
limited by the scope at all.

Here's an example:

Class Test

' member variable:

Public a As String

' method:

Public Sub CreateString

' declare reference:

Dim x As String

' create an object:

x = new String("*"c, 42)

' copy reference to member variable:

a = x

End Sub

End Class


Now, if you call the method CreateString, it will create a string and
store the reference in the variable x. It then copies the reference to
the member variable a. When the method ends, the variable x goes out of
scope and doesn't exist any more, but the string will still exist as
there is still a reference to it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top