I
itarizin
Today I found as ignorance owe me..
Let me explain what I'm tring to do and fail (fail: in my needs)
For example, I've my stupid class:
public class Hello
{
private int x = 0;
public Hello()
{ }
public int Ics
{
get { return this.x; }
set { this.x = value; }
}
}
Perfect, now suppose, to call from a form another form and give its an
instance of our Hello class:
Hello m_hello = new Hello();
m_hello.Ics = 5;
MyForm m_frm = new MyForm();
// Assume that Data is a MyForm property type object
m_frm.Data = m_hello;
m_frm.ShowDialog(this);
Great, now from the other side, I mean the open MyForm, at OnLoad I'll
do a cast from Data to Hello than I'll modify Ics property and will
close the form WITHOUT any reassignment of Data (HIT!).
// On Load
Hello m_frmhello = (Hello)this.Data;
m_frmhello.Ics = 10;
this.Close();
On back to main form, the Hello instance (m_hello) will have the new
Ics value!! I mean will be "10".
That looks like a ref pass to Data, when it's the last thing I want.
Instead, passing to Data an object kinda int, string, bool... or a
struct, this pass will be NOT by reference. This means that Ics will
be however "5".
Can anyone can explain me why? It's cause heap and stack ? There's a
way to avoid this behave without cloning my Hello instance at the
passing moment?
Thanks one billion or maybe more
Let me explain what I'm tring to do and fail (fail: in my needs)
For example, I've my stupid class:
public class Hello
{
private int x = 0;
public Hello()
{ }
public int Ics
{
get { return this.x; }
set { this.x = value; }
}
}
Perfect, now suppose, to call from a form another form and give its an
instance of our Hello class:
Hello m_hello = new Hello();
m_hello.Ics = 5;
MyForm m_frm = new MyForm();
// Assume that Data is a MyForm property type object
m_frm.Data = m_hello;
m_frm.ShowDialog(this);
Great, now from the other side, I mean the open MyForm, at OnLoad I'll
do a cast from Data to Hello than I'll modify Ics property and will
close the form WITHOUT any reassignment of Data (HIT!).
// On Load
Hello m_frmhello = (Hello)this.Data;
m_frmhello.Ics = 10;
this.Close();
On back to main form, the Hello instance (m_hello) will have the new
Ics value!! I mean will be "10".
That looks like a ref pass to Data, when it's the last thing I want.
Instead, passing to Data an object kinda int, string, bool... or a
struct, this pass will be NOT by reference. This means that Ics will
be however "5".
Can anyone can explain me why? It's cause heap and stack ? There's a
way to avoid this behave without cloning my Hello instance at the
passing moment?
Thanks one billion or maybe more