How to pass an object between forms.

M

Matt

Ok so I have this object that I create on Form 1.
Form 1 in turn calls Form 2 that needs a copy of that object to do its
work.

Is there any easy way that I can copy or pass that Form 1 object to
Form 2? I don't want to make it a global object.
 
P

Phill W.

Matt said:
Ok so I have this object that I create on Form 1.
Form 1 in turn calls Form 2 that needs a copy of that object to do its
work.

How about this?

Class Form1
Inherits Form
.. . .
Sub X( ... ) Handles Button1.Click
' this object that I create on Form 1
Dim thing as New CustomObject()

' Form 1 in turn calls Form 2 ...
Dim f2 as New Form2()

' ... that needs a copy of that object
f2.ThingProperty = thing

f2.Show()
End Sub

Class Form2
Inherits Form

Public WriteOnly Property ThingProperty() as CustomObject
Set( value as CustomObject )
m_oThing = value
End Set
End Property
Private m_oThing as CustomObject = Nothing

OK, the above passes a reference to the SAME object in Form1 to
Form2; if you really want to pass a /copy/ of the object, you'll have
to create your own Clone method.

HTH,
Phill W.
 
C

Cor Ligthert [MVP]

Matt,

I go forever for the method showed by Julia and Phill.

An alternative is open your designer part of the accepting form and overload
the New method in that.

I hope this helps,

Cor
 
P

Phill W.

I go forever for the method showed by Julia and Phill.

An alternative is open your designer part of the accepting form and
overload the New method in that.

Cor,

I thought the Forms Designer insisted on using the default Constructor ...

Doesn't creating an overloaded Constructor "confuse" the poor little thing?

TIA,
Phill W.
 
M

Matt

I'm going to give Phill's suggestion a try over the weekend. I'll let
you guys know how it turns out. Thanks again for all the help!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top