Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / January 2008

Tip: Looking for answers? Try searching our database.

Passing an object back to a form

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
BobRoyAce - 16 Jan 2008 16:12 GMT
I am developing an application in VS2005, using VB.NET.

Suppose that I have a class, called Widget, which contains, among
other things, a List(Of WidgetDetail), where WidgetDetail is another
class.

Now, let's say that I have a Widget form, called WidgetForm, and that
it is used to enter Widgets. This form currently has a private module-
level variable, called m_oCurrentWidget which is an instance of
Widget. On WidgetForm, there is a button to add a new WidgetDetail to
be associated with the Widget being worked on currently on
WidgetForm.

When the user clicks on this button, another form, called
WidgetDetailForm, is shown that is used to enter information for a
WidgetDetail. The WidgetDetailForm form currently has a private module-
level variable, called m_oCurrentWidgetDetail, which is an instance of
WidgetDetail.

Well, what I want to do when the user clicks on the button, on the
WidgetDetailForm form, to confirm addition of the WidgetDetail, is to
add the WidgetDetail object defined on WidgetDetailForm, to
m_oCurrentWidget's WidgetDetailList (e.g.
m_oCurrentWidget.WidgetDetailList.Add(m_oCurrentWidgetDetail)). Given
that m_oCurrentWidget and m_oCurrentWidgetDetail are on two different
forms, what is the best way to implement something like this?

The more general question is, what's the best way to pass back an
Object from a form on which it is created?
Leon Mayne - 16 Jan 2008 16:47 GMT
> The more general question is, what's the best way to pass back an
> Object from a form on which it is created?

You could do something like add a readonly property to the child form of
type WidgetDetail called something like SelectedWidget, and then after
calling ChildForm.Show() just pull out the object from the property:

Dim ChildForm as New WidgetForm()
ChildForm.Show(true)
m_oCurrentWidget.Add(ChildForm.SelectedWidget)
BobRoyAce - 16 Jan 2008 17:31 GMT
Thanks Leon...

I was thinking something like that. However, what confuses me is that
I am attempting to reference the readonly property of ChildForm AFTER
it has been shown (i.e. AFTER it has CLOSED). How is it that I'm able
to reference that property after the ChildForm is closed? Is it still
in memory? If yes, when does it go away?
BobRoyAce - 16 Jan 2008 17:33 GMT
I should have mentioned that I will be be doing ChildForm.ShowDialog
rather than ChildForm.Show.
Leon Mayne - 16 Jan 2008 17:49 GMT
>I should have mentioned that I will be be doing ChildForm.ShowDialog
> rather than ChildForm.Show.

Remember that when showing a form you are just calling a method on a class
you have instantiated, consider the code I posted before (edited to use
ShowDialog):

Dim ChildForm as New WidgetForm()
ChildForm.ShowDialog()
m_oCurrentWidget.Add(ChildForm.SelectedWidget)

The ChildForm object has not gone out of scope or otherwise been destroyed,
its visibility has nothing to do with its existence (except as far as the
user is concerned).
BobRoyAce - 16 Jan 2008 18:56 GMT
> The ChildForm object has not gone out of scope or otherwise been destroyed,
> its visibility has nothing to do with its existence (except as far as the
> user is concerned).

Good point. For some reason I thought that when a form "closed", it
"went away". That was silly of me. So, if I wanted to be all neat and
tidy about things, I could add a line

ChildForm = Nothing

to facilitate freeing the memory (without just waiting for garbage
collection to do it), I suppose. I would, of course, do this after
pulling any information I need from ChildForm.

Thanks...
Jack Jackson - 16 Jan 2008 19:41 GMT
>> The ChildForm object has not gone out of scope or otherwise been destroyed,
>> its visibility has nothing to do with its existence (except as far as the
[quoted text clipped - 11 lines]
>
>Thanks...

Setting the reference to Nothing does not cause the object to be
disposed.  Only calling the object's Dispose method does that.

If the ChildForm variable has scope beyond the current method, then
setting it to Nothing will make the object available for garbage
collection sooner.
RobinS - 18 Jan 2008 06:00 GMT
I disagree with this approach. A cleaner approach would be to add a public
property to the parent form, and set it in the child form (to which you
would pass a reference to the parent form). It's best not to check
properties in the child form after you've closed it.

An even cleaner approach would be to have the parent call a method in the
child form that returns what you want, and have the child form show itself
and return the list, like this:

In the child:

   Private newWidget as oWidget = Nothing

   Public Function GetWidget() As oWidget

       Me.ShowDialog()

       If (Me.DialogResult = Windows.Forms.DialogResult.OK) Then _
           Return newWidget
       Else
           Return Nothing
       End If

   End Function

   Private Function SaveStuff()
       'validate? this would run when the user hits OK
       Me.DialogResult = Windows.Forms.DialogResult.OK
   End Function

In the parent:

   Dim getWidget as oWidget

   getWidget = (New childForm).GetWidget   'this will show the childForm

   If (getWidget Is Nothing)
       'they cancelled
   Else
       'add it to your list
   End IF

RobinS.
---------------------------------
>> The more general question is, what's the best way to pass back an
>> Object from a form on which it is created?
[quoted text clipped - 6 lines]
> ChildForm.Show(true)
> m_oCurrentWidget.Add(ChildForm.SelectedWidget)
Leon Mayne - 18 Jan 2008 09:17 GMT
> An even cleaner approach would be to have the parent call a method in the
> child form that returns what you want, and have the child form show itself
[quoted text clipped - 32 lines]
>        'add it to your list
>    End IF

That's not really much different than the solution I posted, apart from (as
you stated correctly) checking the form status which I left out, as you
would have to set this in your OK and Cancel button's event handlers on the
child form.
AlexW - 18 Jan 2008 21:07 GMT
> I disagree with this approach. A cleaner approach would be to add a public
> property to the parent form, and set it in the child form (to which you
[quoted text clipped - 52 lines]
>
> - Show quoted text -

I agree and any other object could call it as well without hardcoding
the child to work with one form. Plus making a reference call to a
memory released object just is bad in theory and makes the your code
read harder.
Leon Mayne - 21 Jan 2008 08:26 GMT
> Plus making a reference call to a
> memory released object just is bad in theory and makes the your code
> read harder.

The class will not have been released from memory after .ShowDialog() has
been called.

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.