473,387 Members | 3,821 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Scope misunderstanding

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
7 1179
>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
"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
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

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

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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Matt Knepley | last post by:
I must be misunderstanding how Python 2.3 handles lexical scoping. Here is a sample piece of code: def run(): a = 1 def run2(b): print a run2(2) print a run()
2
by: bardos | last post by:
Hello, I'm relatively new to C++ so I hope you can help clear up a problem. You may well be able to do so before checking the example, so don't be put off by the long post :) The problem regards...
4
by: BBFrost | last post by:
We have a data entry application written within Framework 1.0 that contains 20+ custom user controls. The controls overlay each other so that only one custom control is visible at a time. What...
7
by: Seth | last post by:
I have noticed that the id of my session object changes when I switch from a non-secure to a secure connection. What I'm trying to do: I have a cookie that is built on the non-secure side of...
2
by: ks | last post by:
Hello, I'm kinda new to PHP and I'm running into something that just seems odd. There's a good chance I'm just doing something wrong though. I'm trying to implement a simple singleton: private...
5
by: strawberry | last post by:
In the function below, I'd like to extend the scope of the $table variable such that, once assigned it would become available to other parts of the function. I thought 'global $table;' would solve...
5
by: teddysnips | last post by:
Is this a bug? I open a DataReader dr from a SQL Server SPROC Do While dr.Read Dim ClubDay As New ChildrensClubDay With ClubDay Dim MPInfo As PaymentInformation If Not dr.GetDateTime(1) =...
3
by: =?Utf-8?B?U2N5dGhlbg==?= | last post by:
Hi all, I have some code similar to the following: C# MyClass mVer = ne w MyClass(); …in a member function mVar.SendEvent( new Event() );
3
by: John Dann | last post by:
Trying to learn Python here, but getting tangled up with variable scope across functions, modules etc and associated problems. Can anyone advise please? Learning project is a GUI-based...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.