Re: How to pass information, classes between forms in Windows Application mode



raylopez99 <raylopez99@xxxxxxxxx> wrote:
Indeed, because your code wasn't how you originally described it. You
claimed an inheritance relationship which simply wasn't there.

Yes, it's true, I did have three different problems, because before
hitting upon the solution, which was the parametrical constructor, I
tried various things like setting Form2:Form1.

BTW, I don't think setting Form2 to inherit from Form1 will solve the
problem without the use of a parametrical constructor, as I thought I
tried this, but maybe it will--time to do a quick test of this thesis
before I post...time out while I check this...making sure all
variables are public in both the base class and derived class... OK,
after many permutations, I confirm you cannot "see" any derived class
in Form 2 that's been instantiated in Form1, either, whether or not
Form2: Form1 or Form2: Form, unless you pass the derived class in the
parametized constructor, just like I said in this thread.

If Form2 : Form1, then from Form2 you will be able to see any variables
in Form1 which are declared as public or protected (or internal if
they're in the same assembly).

Just passing a parameter doesn't mean that Form2 is magically able to
see Form1.myClass1. Form2's constructor will be able to see the
parameter, which starts off with the same value as Form1.myClass1, but
you can't actually refer to the Form1.myClass1 variable from Form2.

I think we're talking two different things. If, referring to this
passage:

(#1) And just to make the point clearer, Form2 must have as a
constructor something besides the normal (default) non parameter
constructor, along these lines:

public Form2(Class1 passClass1_from_Form1)
{
myClass2_that_exists_in_Form2 = passClass1_from_Form1; //
names are arbitrary of course
InitializeComponent();
// other stuff here if you want
}

Alternatively, Form2 could have a parameterless constructor and you
could set properties, or call methods to pass it the information.
Constructors aren't the only way to pass information between objects.

I'm not entirely sure you're correct [UPDATE: after running the tests
below, I'm sure that a person--not you--but a person who says an
object that is passed to Form2 using the parametric constructor of
Form2 from Form1 is not passing the true object but a copy of the
object is not correct]

You're neither passing "the true object" nor a copy of the object.
You're passing a reference to the object.

if you say myClass2_that_exists_in_Form2 cannot
"actually refer to the Form1.myClass1 'variable' from Form2, since we
are passing by value, so it must refer to it.

No, not at all. You have passed the current value of the variable as
the argument. If you change the value of Form1.myClass1, there will be
no way for Form2 to retrieve it. It doesn't have access to the
*variable*.

But I think you're
trying to say [THIS IS WHAT I TESTED BELOW--IT MAY NOT BE WHAT YOU
WERE SAYING, BUT IT WAS INTERESTING FOR ME--RL] it cannot permanently
change the variables (let's assume they are all public) in
Form1.myClass1 since we are not using the 'ref' keywords to pass by
reference.

Passing by reference would effectively give temporary (i.e. duration of
the constructor) access to the variable, yes. However, you can't take
an arbitrary reference to an instance of Form1 and access its myClass1
variable from Form2, while the variable is private.

But, as we know, if you add the "ref" keyword, and with a
helper function we can change a class (we can put the helper function
in another class, or as a standalone function, that accepts 'ref
Class2'), however, the issue here is (and this may or may not be what
you have in mind) whether to use the above example
"myClass2_that_exists_in_Form2" can permanently change any public
variable in class "passClass2[1]_from_Form1", via changes made in
Form2, rather than Form1. [CHANGED TO '2' from '1' to be consistent]

Reference parameters do effectively change the situation somewhat, but
it's important to note that Form2 doesn't really know that it's
Form1.myClass1 that's been passed to it by reference. It could be a
local variable, or a variable from something completely different.

There's only one way to find out, and that's to test this
hypothesis...timeout while I test this...I will pass a 'myClass2' from
Form1 to Form2, using the parametric constructor, then change a public
variable of myClass2 in Form2, then see if it's changed when I go back
to Form1...the hypothesis being tested is that it's probably true that
no change will be made to the Form1 'myclass' [UPDATE: AND THIS
'HYPOTHESIS' IS WRONG-RL]--but let's see...

Ah, OK, about 60 minutes later...found out you're WRONG AGAIN JONNY!!!
AHAHAHAHA. Hey just kidding, we're talking (I hope) about two
different things.

Here's what I did, which probably will meet with an objection or two,
but here goes: in Form1 I instantiated two classes, class 2 [call it
"OriginalClass2"], and class 3, see below at **1**. Class 3 is a
'helper' class that changes public variables in class 2 by two
methods, one method that takes a 'ref' keyword (pass by ref) {public
void refMethodChangeClass2 (ref Class2 x)} and the other method,
{noRefMETHODCl2(Class2 y)}, that changes public variables in class 2
by taking class 2 via pass-by-value, each method accepting a Class2
object.

<snip>

Rather than me explaining reference parameters here, I suggest you read
my article on them:
http://pobox.com/~skeet/csharp/parameters.html

You still seem confused about the difference between passing a
reference by value and passing a parameter *by* reference. Changing the
*contents* of an object doesn't change the value of a variable which
refers to that object.

Hopefully the article above will clarify things for you.

<snip>

Um, no. A forward declaration in C++ is not the same thing as a
variable declaration at all.

OK, you win on this point. My bad. I'll call it a 'variable
declaration' even though a class object is being declared, not a
primitive data like int, double, etc, which I like to think as
'variables' in my mind's eye.

You should adjust your meaning of the word variable then. And no, a
class *object* isn't being declared - a *variable* is being declared.
Just because there's a variable doesn't mean any object is necessarily
being created. The variable may have a null value, or refer to an
object which was created elsewhere. For instance:

StringBuilder sb1 = new StringBuilder();
StringBuilder sb2 = sb1;

That has declared two variables, but only one object has been created.

--
Jon Skeet - <skeet@xxxxxxxxx>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
.



Relevant Pages

  • Re: NEWBIE - Accessing Variables stored in another form
    ... No - How do I create this reference? ... I am opening a Form2 from Form1. ... I want to retrieve values from ...
    (microsoft.public.dotnet.languages.vb)
  • Re: How to pass information, classes between forms in Windows Application mode
    ... parametricized or parameter constructor, ... Form2: Form1 as you discuss below and as I further comment below. ... You're passing a reference to the object. ...
    (microsoft.public.dotnet.languages.csharp)
  • RE: Urgent, plz help
    ... Then form1 has a reference to form2 but not the ... One way to over come that is to pass a reference to the object ... that you want to bind in form1 to the constructor of form2. ... write new constructor for form2 that take a textbox(textboxes). ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: DataSet Access
    ... I can't tell from your post if Form2 is loaded already when you make ... the update in form1 or not but it's somethign to look into. ... Then I reference can reference it from anywhere. ... breakpoint in Form2, verify the number. ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: How to pass information, classes between forms in Windows Application mode
    ... purposes it's the same as the parameterized copy constructor, ... Another way is the inheritance of forms, Form2: ... Form1 as you discuss below and as I further comment below. ... Form2 from Form1 is not passing the true object but a copy of the ...
    (microsoft.public.dotnet.languages.csharp)