471,319 Members | 1,427 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,319 software developers and data experts.

How to pass objects between forms?

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

Feb 5 '06 #1
3 8429
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

Feb 5 '06 #2
swb76 <sw***@yahoo.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 5 '06 #3

Jon wrote:
swb76 <sw***@yahoo.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


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

- Sam

Feb 6 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Zlatko Matiĉ | last post: by
2 posts views Thread by Chane | last post: by
3 posts views Thread by deko | last post: by
6 posts views Thread by DMUM via AccessMonster.com | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.