By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,816 Members | 1,332 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,816 IT Pros & Developers. It's quick & easy.

Pass an object by Reference to another form?

P: n/a
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
Share this Question
Share on Google+
11 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a


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

P: n/a
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

P: n/a
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

P: n/a


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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.