Home All Groups Group Topic Archive Search About

Scope misunderstanding

Author
5 Nov 2006 9:00 PM
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

Author
5 Nov 2006 11:24 PM
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

--
Mattias Sjögren [C# MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Author
6 Nov 2006 3:14 AM
Michael C
"edamron" <edamron@spamcop.net> wrote in message
news:1162760430.138139.142000@h54g2000cwb.googlegroups.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
Author
6 Nov 2006 2:21 PM
Chris Dunaway
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?
Author
7 Nov 2006 11:39 PM
Michael C
"Chris Dunaway" <dunaw***@gmail.com> wrote in message
news:1162822908.327493.208130@h54g2000cwb.googlegroups.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 :-)

Show quoteHide quote
>
Author
6 Nov 2006 8:22 PM
edamron
Michael C wrote:
Show quoteHide quote
> "edamron" <edamron@spamcop.net> wrote in message
> news:1162760430.138139.142000@h54g2000cwb.googlegroups.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?
Author
6 Nov 2006 9:23 PM
Göran_Andersson
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.
Author
6 Nov 2006 8:22 PM
edamron
Michael C wrote:
Show quoteHide quote
> "edamron" <edamron@spamcop.net> wrote in message
> news:1162760430.138139.142000@h54g2000cwb.googlegroups.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?