473,595 Members | 2,626 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 8658
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.co m> 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.co m>
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.co m> 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.co m>
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
4165
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 < document.forms(0).item.length; i++) { dosomethingwith(document.forms(0).item(i)); }
11
8793
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
21604
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 server ? Is it possible to use parameters in pass-through queries ? An additional question: Is it possible to connect to a database on MySQL or PostgreSQL using ADO ? Is it possible to execute pass-through queries with parameters, using ADO...
0
3313
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 appear when someone is trying to use it as front-end for real server database systems such as PostgreSQL or MySQL. One of these problems is regarding pass-through queries and parameters. I wanted to have all the code on client, while executing it on...
2
2031
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 for second form and showed as modaless. And i entered some text in second form's textbox. While i clicked the seconds form's button the value in textbox of second form to be transfered to first form textbox. Thanx
3
2031
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
1257
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 between forms like I'd do in VB. Often I'd use a module with public variables...in VC++ I'd use member variables... How can I do this in C#? Any hints/help appreciated. TIA
6
5962
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 forms in the database that will use this function, so I need to be able to tell the code which form to unlock. What I have is as follows: Public Function akeyEdit()
12
11051
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. Here is a newbie mistake that I found myself doing (as a newbie), and that even a master programmer, the guru of this forum, Jon Skeet, missed! (He knows this I'm sure, but just didn't think this was my problem; LOL, I am needling him) If...
0
8260
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8017
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8250
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6671
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5837
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5416
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2391
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1490
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1222
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.