[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? ...
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.
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;
}
}
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 ;)
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:
//simple form with two buttons and two textboxes.
private void button1_Click(object sender, EventArgs e)
{
textBox2.Text = "OriginalText";
modifybyvalue(textBox1, textBox2);
MessageBox.Show("Textbox2.text = " + textBox2.Text);
}
private void modifybyvalue(TextBox TB1,TextBox TB2)
{
TB1.Text = "Hello";
TB2 = TB1;
TB2.Text = "Goodbye";
}
private void modifybyref(TextBox TB1, ref TextBox TB2)
{
TB1.Text = "Hello";
TB2 = TB1;
TB2.Text = "Goodbye";
}
private void button2_Click(object sender, EventArgs e)
{
textBox2.Text = "OriginalText";
modifybyref(textBox1, ref textBox2);
MessageBox.Show("Textbox2.text = " + textBox2.Text);
}
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
http://img125.echo.cx/img125/3937/01nocomment8so.gif
Re: by value and by ref question related to passing forms
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!
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
Re: by value and by ref question related to passing forms
Quote:
Originally Posted by JPicasso
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!