Reputation: 595
I have a win form window named Form1 with - a label named Label1, two buttons named button1 and button2 respectively.
When i click on button1, it creates a new window of type Form1 named f, which is inintialized to current window, i.e. this (Form1), f is the sent to a method named ChangeByReference as reference.
ChangeByReference method changes the form's text, Label1's text & ForeColor which is passed by reference
When i click on button2, this instance method named ChangeByValue as value.
ChangeByValue method creates a new window an initialized to default constructor of Form1, which is then set to parameter valForm, i change some properties and finally show it using Show method.
Now the question is why is ChangeByValue method call is destroying the orginal window and showing the new form since it's parameter is passed by value ?
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.Text = "Original Window";
}
public void ChangeByReference(ref Form1 refForm)
{
Form1 f = refForm;
f.label1.Text = "This is changed by Reference";
f.label1.ForeColor = Color.Aquamarine;
}
public void ChangeByValue(Form1 valForm)
{
Form1 f = new Form1();
f = valForm;
f.Text = "Changed Window";
f.label1.Text = "This is changed by Value";
f.label1.ForeColor = Color.Red;
f.Show();
}
private void button1_Click(object sender, EventArgs e)
{
Form1 f = this as Form1;
ChangeByReference(ref f);
}
private void button2_Click(object sender, EventArgs e)
{
ChangeByValue(this);
}
}
Upvotes: 0
Views: 1737
Reputation: 7517
The method ChangeByReference
doesn't really need the ref
. You are not using the "feature" of ref
. I think you are mixing two concepts, one is "reference types" and second is "passing parameters as reference". Also per documentation:
Do not confuse the concept of passing by reference with the concept of reference types. The two concepts are not the same. A method parameter can be modified by ref regardless of whether it is a value type or a reference type. There is no boxing of a value type when it is passed by reference.
Source: MSDN - ref (C# Reference)
In your code, you just change some properties of the form you pass as parameter. It has the same effect whether you specify the parameter as ref
or not.
In your second method ChangeByValue
, you first initialise a new Form1
object, but right after you re-assign the local variable to the parameter value. This way, the new object won't be used anymore (you "threw" it away). So the next lines of code, you are actually changing / setting the properties of the form you provided through the parameter. In this case (coming from the button2_Click
event), you are changing your current form.
An interesting article: Parameter passing in C# (by Jon Skeet).
Upvotes: 2
Reputation: 6463
Passing the valForm
argument by value means that changing the reference of valForm
will not change the reference of the passed variable (in your example it's this
).
In your method you are assigning to f
a reference to exactly the same object as valForm
references to, which means both variables point to the same object, which is this
. If you modify that object via one reference, it is reflected in the other.
Upvotes: 1