473,385 Members | 1,355 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 8633
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Andrés Giraldo | last post by:
Hi! I'm trying to pass trough all the objects of a form but I have some text inputs in a DIV and I have many DIVs like this on my form. I'm doing something like: for (i = 0; i <...
11
by: Vanessa | last post by:
Hi, I would like to know whether there's any way for me to pass an object by reference to another form? Regards Vanessa
7
by: Zlatko Matiĉ | last post by:
Let's assume that we have a database on some SQL server (let it be MS SQL Server) and that we want to execute some parameterized query as a pass.through query. How can we pass parameters to the...
0
by: Zlatko Matiĉ | last post by:
Hi everybody! Recently I was struggling with client/server issues in MS Access/PostgreSQL combination. Although Access is intuitive and easy to use desktop database solution, many problems...
2
by: Chane | last post by:
hi i have doubt in how to pass values back to the called form. I have two forms, each having a textbox and a button control. First i'm run the first form and click the button, it creates object...
3
by: deko | last post by:
I have a Solution with 3 Projects, representing 3 layers: App_BL App_DA App_UI All in Namespace APP UI is a Windows Forms app and BL and DA are class libraries.
2
by: Ed Ardzinski | last post by:
I'm basically a VB programmer taking my first steps in C#. I have a little C++ experience, so I'm not completely lost. I am having a tought time figuring out exactly how to emulate passing data...
6
by: DMUM via AccessMonster.com | last post by:
Hello I am trying to pass the name of my subform to a function/sub but I can't seem to get it to work. I am using an autokey function (ctrl E) to unlock text boxes on a subform. I have a few...
12
by: raylopez99 | last post by:
Keywords: scope resolution, passing classes between parent and child forms, parameter constructor method, normal constructor, default constructor, forward reference, sharing classes between forms....
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.