473,407 Members | 2,676 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,407 software developers and data experts.

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 8760
Vanessa <va*****@eclinic.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.com>
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*****@eclinic.com.sg> wrote in message
news:eQ*************@TK2MSFTNGP10.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*****@eclinic.com.sg> wrote in message
news:eQ*************@TK2MSFTNGP10.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*******@hotmail.com> wrote in message
news:#S**************@TK2MSFTNGP09.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*****@eclinic.com.sg> wrote in message
news:eQ*************@TK2MSFTNGP10.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@nospam-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.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #9
Rick <rrquick@nospam-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.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #10


Jon Skeet wrote:
Rick <rrquick@nospam-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.


True, I did think about that but then I can't think of a reason why (in
her situation) she would want to keep an instance being passed around
for a convenient class wrapping the DataSet. Her requirement was to keep
it consistent by making any change in another form reflect back to the
other (or others). Since it's only a DataSet and not a user defined
class which could grow into something more meaningful or useful,
requiring separate instances, I believe a singleton is probably the
best, clean choice. Then again, that's only my honest opinion :)

Rick

Jul 21 '05 #11
Rick <rrquick@nospam-com> wrote:
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.
True, I did think about that but then I can't think of a reason why (in
her situation) she would want to keep an instance being passed around
for a convenient class wrapping the DataSet.


Well, there's nothing to say for sure that there couldn't be multiple
instances of forms A and B, with each pair sharing a single DataSet.
Her requirement was to keep
it consistent by making any change in another form reflect back to the
other (or others). Since it's only a DataSet and not a user defined
class which could grow into something more meaningful or useful,
requiring separate instances, I believe a singleton is probably the
best, clean choice. Then again, that's only my honest opinion :)


I tend to shy away from using Singletons unless it's a real design
decision to go for them, rather than just as a convenience.

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

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

Similar topics

12
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,...
110
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...
3
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...
4
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...
4
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...
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: 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
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....
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....
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
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...
0
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,...
0
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...
0
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,...
0
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...

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.