473,753 Members | 6,868 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 8670
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
4186
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
8813
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
21633
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
3322
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
2037
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
2038
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
1264
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
5975
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
11105
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
9072
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8896
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9653
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...
0
9451
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9333
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
6151
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();...
0
4942
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2872
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2284
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.