Hi,
I would like to know whether there's any way for me to pass an object by
reference to another form?
Regards
Vanessa 11 7774
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
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
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
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
"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
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
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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
|
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...
|
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
|
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
|
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();
| |
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 ;-)
|
by: David++ |
last post by:
Hi folks,
I would be interested to hear peoples views on whether or not 'pass by
reference' is allowed when using a Web Service method.
The thing that troubles me about pass-by-reference into a WebService is that
essentially we are passing an address of an object which resides on the
'local machine' i.e. a local machine object address. Surely when the
WebService method is called and run 'on the server', the reference type will
be...
|
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();
|
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...
|
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...
|
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...
| |
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,...
|
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...
|
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,...
|
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...
|
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();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |