|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Scope misunderstandingprofound 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 >However isn't it true that when the Load No. The lifetime of an object doesn't directly depend on the scope of>sub exits these object will be destroyed?? 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. "edamron" <edamron@spamcop.net> wrote in message I think what you don't understand (if I understand correctly) is that it is 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. 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 Michael C wrote:
> Private Sub FormMain_Load(...) Handles MyBase.Load Are you channeling the spirit of a VB6 guru?> Dim x as MyObject > set x = new MyObject > set y = x > End sub "Chris Dunaway" <dunaw***@gmail.com> wrote in message I've mainly used c# in dotnet so my vb7 syntax might be a little vb6ish :-)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? Show quoteHide quote > Michael C wrote:
Show quoteHide quote > "edamron" <edamron@spamcop.net> wrote in message Thanks for the reply Michael.> 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 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? edamron wrote:
> It seems to me that he IS Dim the objects in the load event handler Not if there is a reference to the object stored somewhere else.> isn't he? So when that event finishes and exits wouldn't all of the > objects he Dim'd be eligible for garbage collection? 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. Michael C wrote:
Show quoteHide quote > "edamron" <edamron@spamcop.net> wrote in message Thanks for the reply Michael.> 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 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?
This mail sending code snippet does not work
Accessing an arraylist from multiple forms. Examples of VB.net commercial software packages Visual Basic 2003 won't run - get error message Dynamically changing button label from a variable regex.replace and trim Application.EnableVisualStyles() still required in version 2.0 Initialize DefaultPropertyAttribute for a Object property coment a block of code Do I need to reset the cursor or will that happen automatically when I leave the subroutine? |
|||||||||||||||||||||||