How to pass objects between forms?

S

swb76

I have a Object Oriented question.
I got two forms - Form1 and Form2. I also got two classes Class1 and
Class2

Form1 has objects Object1 - instance of Class1 and Object2 - instance
of Class2.

When i load Form1, i load this form and instantiate Object1 and
Object2. Please note Object1 and Object2 can be fairly large because of
several variables.

On a button on Form1, i want to load Form2 and on it is a listbox that
would display the contents of Object1 and Object2.

So my Form2 also has variables - Object1 and Object2 defined.

Now if i pass these objects in the constructor to Form2, will Form2
create a copy of these objects in memory or will it reference the same
object of Form1??

In short, How do i make Form2 reference the same objects (Object1 and
Object2) of Form1.

I know how CSharp does it. Would it make two copies of Object1, one for
Form1 and one for Form2?

Any help is greatly appreciated.

Thanks,
- Sam
 
L

Light

Please correct me if I am wrong but for C# then when passing objects
that are not value type then it never does a copy on pass but always
passes by pointer (ie by address) and as such performance is always
guaranteed. However, you must pass by ref or out to change the object
pointed to.

Furthermore a form is just a class and wouldn't operate any different
therefore it will not make a copy in memory.
Curtis
http://www.ghostclip.com
The Premier Help System For Developers
 
J

Jon Skeet [C# MVP]

swb76 said:
I have a Object Oriented question.
I got two forms - Form1 and Form2. I also got two classes Class1 and
Class2

Form1 has objects Object1 - instance of Class1 and Object2 - instance
of Class2.

When i load Form1, i load this form and instantiate Object1 and
Object2. Please note Object1 and Object2 can be fairly large because of
several variables.

On a button on Form1, i want to load Form2 and on it is a listbox that
would display the contents of Object1 and Object2.

So my Form2 also has variables - Object1 and Object2 defined.

Now if i pass these objects in the constructor to Form2, will Form2
create a copy of these objects in memory or will it reference the same
object of Form1??

If they are classes rather than structs (reference types rather than
value types) then the variables will be holding references rather than
values, and those references are what will be passed.

It's very important that you understand the difference between value
types and reference types.

See http://www.pobox.com/~skeet/csharp/parameters.html and
http://www.pobox.com/~skeet/csharp/memory.html for more information.
 
S

swb76

Jon said:
If they are classes rather than structs (reference types rather than
value types) then the variables will be holding references rather than
values, and those references are what will be passed.

It's very important that you understand the difference between value
types and reference types.

See http://www.pobox.com/~skeet/csharp/parameters.html and
http://www.pobox.com/~skeet/csharp/memory.html for more information.

Jon,
Thanks for pointing those articles. They were very helpful.

- Sam
 

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