By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,515 Members | 1,042 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,515 IT Pros & Developers. It's quick & easy.

Scope misunderstanding

P: n/a
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.ShowSalaryToolStripMenuIt em, _
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

Nov 5 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
>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

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 5 '06 #2

P: n/a
"edamron" <ed*****@spamcop.netwrote in message
news:11**********************@h54g2000cwb.googlegr oups.com...
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
Nov 6 '06 #3

P: n/a
Michael C wrote:
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?

Nov 6 '06 #4

P: n/a

Michael C wrote:
"edamron" <ed*****@spamcop.netwrote in message
news:11**********************@h54g2000cwb.googlegr oups.com...
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
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?

Nov 6 '06 #5

P: n/a

Michael C wrote:
"edamron" <ed*****@spamcop.netwrote in message
news:11**********************@h54g2000cwb.googlegr oups.com...
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
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?

Nov 6 '06 #6

P: n/a
edamron wrote:
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.
Nov 6 '06 #7

P: n/a
"Chris Dunaway" <du******@gmail.comwrote in message
news:11**********************@h54g2000cwb.googlegr oups.com...
Michael C wrote:
> 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?
I've mainly used c# in dotnet so my vb7 syntax might be a little vb6ish :-)
>

Nov 7 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.