Pass an object by Reference to another form?

  • Thread starter Thread starter Vanessa
  • Start date Start date
V

Vanessa

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

Regards
Vanessa
 
Vanessa said:
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.
 
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
 
Jerry said:
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
 
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
 
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
 
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
 
Vanessa said:
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
 
Rick said:
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.
 
Rick said:
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 said:
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
 
Rick said:
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.
 
Back
Top