Results 1 to 10 of 10

Thread: [RESOLVED] by value and by ref question related to passing forms

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Feb 2001
    Location
    Crossroads
    Posts
    3,046

    Resolved [RESOLVED] by value and by ref question related to passing forms

    I dont understand something. If C# passes by value as the default then why does the text on the startup Form1 change when the following code is executed?

    private Form1 frmMain;
    public Form2(Form1 f)
    {
    InitializeComponent();
    frmMain = f;
    frmMain.Text = "Hello";
    }

    I would have thought that a new Form1 named f would be created and that the text change would occur on that new Form1 named f and not on the Form1 that was shown at startup. What am I misunderstanding? ... or are forms passed by reference? ...
    Last edited by Muddy; Aug 8th, 2006 at 01:05 PM. Reason: resolved

  2. #2
    Fanatic Member JPicasso's Avatar
    Join Date
    Aug 2001
    Location
    Kalamazoo, MI
    Posts
    843

    Re: by value and by ref question related to passing forms

    The default behavior of value parameters is to make a copy of the item passed in.
    With objects, this copy is essentially a copy of the pointer to that object, so it effectivly makes objects "by reference". But for someone who knows the particulars, it's different.

    If you were to re-assign the "f" in your code to another form, you would NOT be changing the referenced object that you passed in. I don't know if that is explained well, but I tried.




    structs and the core data types will function as value types like you are looking for.
    Merry Christmas

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Feb 2001
    Location
    Crossroads
    Posts
    3,046

    Re: by value and by ref question related to passing forms

    Quote Originally Posted by JPicasso
    ... this copy is essentially a copy of the pointer to that object, so it effectivly makes objects "by reference". But for someone who knows the particulars, it's different.
    Interesting ... I always thought that what you just described was the definition of byRef ... how would you define byRef, then? Is it because it is a copy of the pointer being passed and not the actual pointer itself?

    Thanks for the reply!

    edit:

    and why dont variables act the same way? The following shows x=10 which is what I would normally expect sinc it was passed byVal:
    Code:
            public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                long x;
                x = 10;
                tryIt(x);
                MessageBox.Show( Convert.ToString( x));
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
    
            }
            private void tryIt(long x)
        {
            x = 22;
        }
        }
    Last edited by Muddy; Aug 8th, 2006 at 09:52 AM.

  4. #4
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: by value and by ref question related to passing forms

    You don't deal with object as an object, you have to look at it as a "reference" to a block of memory (the block of memory is the object)

    It's just like using pointers in C/C++, you can't change the value the pointer holds but you can change the content of the memory block it points to... But if you want to modify the pointer itself, you pass a pointer to the pointer, so now you can change the content of both the pointer and what it points at.

    It's the same in Object Oriented Methodology, Objects are references, you can do whatever you want to what they point to, but you can't change the value inside the reference (re-instantiate), if you want to do that, you'll have to use a reference of the reference (ref Object)

    I hope that made sense to you
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  5. #5
    Fanatic Member JPicasso's Avatar
    Join Date
    Aug 2001
    Location
    Kalamazoo, MI
    Posts
    843

    Re: by value and by ref question related to passing forms

    I hope I explain this well enough. If not, I hope I at least don't confuse.

    Yes, when you pass a value type by value, you get a complete copy of that value. When you pass a reference type (an object) by value, you get a complete copy of the reference. So, any changes you do through that reference WILL modify the passed reference's object.

    However, if you were to reassign that reference variable (the one pointing to the object) and it was passed by value, you would not alter the original reference variable that was passed into the function.
    If you passed the reference variable by reference, and reassign that reference variable, you lose the original reference.

    Try this:

    VB Code:
    1. //simple form with two buttons and two textboxes.
    2.  
    3.         private void button1_Click(object sender, EventArgs e)
    4.         {
    5.             textBox2.Text = "OriginalText";
    6.             modifybyvalue(textBox1, textBox2);
    7.             MessageBox.Show("Textbox2.text = " + textBox2.Text);
    8.         }
    9.  
    10.         private void modifybyvalue(TextBox TB1,TextBox TB2)
    11.         {
    12.             TB1.Text = "Hello";
    13.             TB2 = TB1;
    14.             TB2.Text = "Goodbye";
    15.         }
    16.  
    17.         private void modifybyref(TextBox TB1, ref TextBox TB2)
    18.         {
    19.             TB1.Text = "Hello";
    20.             TB2 = TB1;
    21.             TB2.Text = "Goodbye";
    22.         }
    23.  
    24.         private void button2_Click(object sender, EventArgs e)
    25.         {
    26.             textBox2.Text = "OriginalText";
    27.             modifybyref(textBox1, ref textBox2);
    28.             MessageBox.Show("Textbox2.text = " + textBox2.Text);
    29.         }
    Merry Christmas

  6. #6
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: by value and by ref question related to passing forms

    Quote Originally Posted by JPicasso
    I hope I explain this well enough. If not, I hope I at least don't confuse.
    OMG
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  7. #7
    Fanatic Member JPicasso's Avatar
    Join Date
    Aug 2001
    Location
    Kalamazoo, MI
    Posts
    843

    Re: by value and by ref question related to passing forms

    oh yeah?

    Merry Christmas

  8. #8

    Thread Starter
    PowerPoster
    Join Date
    Feb 2001
    Location
    Crossroads
    Posts
    3,046

    Re: by value and by ref question related to passing forms

    ok, I obviously need to do a lot of reading on the subject ....

    thanks for all of the replies!

  9. #9
    Fanatic Member JPicasso's Avatar
    Join Date
    Aug 2001
    Location
    Kalamazoo, MI
    Posts
    843

    Re: by value and by ref question related to passing forms

    Sorry if I confused.

    try this link, or search on "reference types"

    http://www.c-sharpcorner.com/Code/20...peandvalue.asp
    Merry Christmas

  10. #10

    Thread Starter
    PowerPoster
    Join Date
    Feb 2001
    Location
    Crossroads
    Posts
    3,046

    Re: by value and by ref question related to passing forms

    Quote Originally Posted by JPicasso
    Sorry if I confused.

    try this link, or search on "reference types"

    http://www.c-sharpcorner.com/Code/20...peandvalue.asp
    Perfect explanation! I think it was probably what you had said in your previous thread, but it finally clicked when i read the info in the link! Big help! Thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width