473,769 Members | 3,567 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Pass an object by Reference to another form?

Hi,
I would like to know whether there's any way for me to pass an object by
reference to another form?

Regards
Vanessa
Jul 21 '05 #1
11 8814
Vanessa <va*****@eclini c.com.sg> wrote:
I would like to know whether there's any way for me to pass an object by
reference to another form?


No - you can't actually pass an *object* (a reference-type object) by
either reference or value. You *can* however pass a reference to such
an object, and you can pass that either by value or by reference.

Confused? Have a look at
http://www.pobox.com/~skeet/csharp/parameters.html which explains it in
a lot more detail. Admittedly it's in C#, but most of it should be
fairly understandable from a VB.NET point of view. Note that there's
nothing particularly special about a form in this case - it's just
another class, and one which happens to derive from
System.Windows. Forms.Form.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #2
Absolutely.
Simply define your object as a parameter by reference in the forms
constructor. In the constructor you will want to make it equal (pass the
objects location in memory) to a variable defined as the objects type in
your forms class and define the scope for that variable within your class.

Regards
"Vanessa" <va*****@eclini c.com.sg> wrote in message
news:eQ******** *****@TK2MSFTNG P10.phx.gbl...
Hi,
I would like to know whether there's any way for me to pass an object by
reference to another form?

Regards
Vanessa

Jul 21 '05 #3
Jerry wrote:
Absolutely.
Don't know what that was for..
Simply define your object as a parameter by reference in the forms
constructor. In the constructor you will want to make it equal (pass the
objects location in memory) to a variable defined as the objects type in
your forms class and define the scope for that variable within your class.


Actually no, you don't have to send it "ByRef".. it'll only add on
another layer of indirection. Like Jon said, variables of reference
types hold references to the actual objects, and sending them simply
ByVal will suffice. That way, the method/class that get's passed the
object's reference will be able to reach the object the same way the
original class could. Also, one can argue that using a ByRef on a
variable of reference type actually "degrades" performance by just a
fraction.

For the OP: Simply pass the object's reference ByVal in the way you'd
pass any other variable.

Rick

Jul 21 '05 #4
I'd also like to add that I've never found any reason why one should
pass an variable of a reference type ByRef (actually I've never had to
myself). Valuetypes, yes ByRef is extremely useful when you need to
modify the original variable itself, by reference types.. I don't think
there's a need at all.

Rick

Jul 21 '05 #5
Can you give us more info? Are you trying to set a property of the form, or
are you defining your own. Have you tried using the constructor yet?

Kirk Graves
"Vanessa" <va*****@eclini c.com.sg> wrote in message
news:eQ******** *****@TK2MSFTNG P10.phx.gbl...
Hi,
I would like to know whether there's any way for me to pass an object by
reference to another form?

Regards
Vanessa

Jul 21 '05 #6
I have 2 forms (Form A & Form B). Form A will Call Form B and needs to pass
in a DataSet to FormB. Whatever I have change in Form B, my form A must be
able to reflect the changes. This is why I need to pass the object into
another form by reference.

Regards
Vanessa

"Kirk" <mc*******@hotm ail.com> wrote in message
news:#S******** ******@TK2MSFTN GP09.phx.gbl...
Can you give us more info? Are you trying to set a property of the form, or are you defining your own. Have you tried using the constructor yet?

Kirk Graves
"Vanessa" <va*****@eclini c.com.sg> wrote in message
news:eQ******** *****@TK2MSFTNG P10.phx.gbl...
Hi,
I would like to know whether there's any way for me to pass an object by
reference to another form?

Regards
Vanessa


Jul 21 '05 #7


Vanessa wrote:
I have 2 forms (Form A & Form B). Form A will Call Form B and needs to pass
in a DataSet to FormB. Whatever I have change in Form B, my form A must be
able to reflect the changes. This is why I need to pass the object into
another form by reference.
You don't need to pass the object by reference, ByVal will suffice (as
explained in an earlier post).

A small tweak to your design:

Why have the DataSet in Form A at all? Why not have an independant class
(a singleton) that holds the DataSet and shares it across Form A and
Form B? This will help you avoid the "passing of object from one form to
another" when there's really no need, and since you require that any
change made from FormB be reflected in FormA, a singleton will do best.

-Rick

Regards
Vanessa


Jul 21 '05 #8
Rick <rrquick@nosp am-com> wrote:
I'd also like to add that I've never found any reason why one should
pass an variable of a reference type ByRef (actually I've never had to
myself).
Here's an example (using out, but the principle is the same):

string fullName = AskUserForName( );
string lastName, firstName;

SplitName (fullName, out firstName, out lastName);

where SplitName does the obvious thing. Basically, wherever you want to
change the value of the variable (rather than just changing the object
itself) and a return value doesn't suffice, pass-by-reference *might*
be useful. (I personally avoid it wherever possible, however.)
Valuetypes, yes ByRef is extremely useful when you need to
modify the original variable itself, by reference types.. I don't think
there's a need at all.


There's no *need* for pass-by-reference at all, in many ways (Java
doesn't have it, for instance) - I believe InterOp would be difficult
without it though.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #9
Rick <rrquick@nosp am-com> wrote:
Why have the DataSet in Form A at all? Why not have an independant class
(a singleton) that holds the DataSet and shares it across Form A and
Form B?


I don't see why a singleton is needed here at all. I would suggest
creating a new instance of the independent class, and passing a
reference to that instance to the constructors of both Form A and Form
B. Using a singleton avoids the need for that reference, but could
prove restrictive later on.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
1801
by: Casey | last post by:
Yeah, I know this question was asked by someone elselike 2 weeks ago. But I need some additional help. I have a program I'm developing, and multiple different forms will be opened. For now though, I just have two forms. One is the main window, and the other sets some preferences like names, and email addresses. When someone opens this window, and enters values on this page, I'd like it to set the values of public variables on the main...
110
9954
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object must be an object instead of
3
3889
by: Lyn | last post by:
Hi, I have been experiencing a problem passing a LIKE statement in the WHERE argument of a DoCmd.Openform statement. I have posted that issue separately. However, in an attempt to work around the problem, I thought of using OPENARGS to pass information to the form being opened (instead of using WHERE). In the parent form, I have generated a RecordSet (RS) with the information
4
1470
by: Vaughn | last post by:
When I pass a reference to my current form, frm_x, when I create and show another form, frm_y, do I have access to all of the public methods, controls, members, etc.. of frm_x from frm_y? In my code, I pass the reference like this: private void btn_ListEmp_Click(object sender, System.EventArgs e) { frm_EmpList frm_empList = new frm_EmpList(this); frm_empList.MdiParent = this.MdiParent; frm_empList.Show();
4
3408
by: z_learning_tester | last post by:
I'm reading the MS press C# book and there seems to be a contradiction. Please tell me which one is correct, 1 or 2. Thanks! Jeff 1. First it gives the code below saying that it prints 0 then 42. They say that 42 is printed the second time since the value was wrapped in a class and therefore became passed by reference. (sorry for any typos I am a newbie here ;-)
11
7773
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
2579
by: Boki | last post by:
Hi All, I can't pass data to another form: in form2: private void button1_Click(object sender, EventArgs e) { Form1 form_copy = new Form1();
12
3018
by: Bryan Parkoff | last post by:
I write my large project in C++ source code. My C++ source code contains approximate four thousand small functions. Most of them are inline. I define variables and functions in the global scope. The global variables and global functions are hidden to prevent from accessing by the programmers. All global functions share global variables. Only very few global functions are allowed to be reusability for the programmers to use. Few...
12
11109
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
9579
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
9416
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,...
1
9981
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
9850
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
8862
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
7396
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
6662
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();...
2
3551
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2810
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.