ref enum to Form constructor

S

sklett

I have a Form that I want to show a user to choose some options. I have set
it up so that Form's ctor takes ref parameters, one of them is an enum. For
example:
<code>
public enum AdjustTimeStyle
{
Undefined = 0,
Smooth
}

AdjustTimeStyle style = AdjustTimeStyle.Undefined;

AdjustTimeOptionsDlg dlg = new AdjustTimeOptionsDlg(ref style, /*other
params*/);
</code>

Now in the ctor I assign style to a member var
<code>
public class AdjustTimeOptionsDlg : Form
{
private AdjustTimeStyle m_style;

public AdjustTimeOptionsDlg(ref AdjustTimeStyle _style)
{
m_style = _style;
}
// other methods, etc
}
</code>


When the user chooses different options in the Form, I change the value of
m_style, when the control closes and I inspect the value of style (passed by
ref to the ctor of the dialog) it's still undefined. I don't get it. Is
this possible?

Thanks for reading,
Steve
 
D

Daniel O'Connell [C# MVP]

sklett said:
I have a Form that I want to show a user to choose some options. I have
set it up so that Form's ctor takes ref parameters, one of them is an enum.
For example:
<code>
public enum AdjustTimeStyle
{
Undefined = 0,
Smooth
}

AdjustTimeStyle style = AdjustTimeStyle.Undefined;

AdjustTimeOptionsDlg dlg = new AdjustTimeOptionsDlg(ref style, /*other
params*/);
</code>

Now in the ctor I assign style to a member var
<code>
public class AdjustTimeOptionsDlg : Form
{
private AdjustTimeStyle m_style;

public AdjustTimeOptionsDlg(ref AdjustTimeStyle _style)
{
m_style = _style;
}
// other methods, etc
}
</code>


When the user chooses different options in the Form, I change the value of
m_style, when the control closes and I inspect the value of style (passed
by ref to the ctor of the dialog) it's still undefined. I don't get it.
Is this possible?

Ref parameters don't work that way. A ref parameter passes a reference to a
variable into the method, but when you store it as a field the actual value
is copied over to that field. It'd be a bad move, logically, to copy the
reference because often times the variable it refers to would be destroyed
when the stackframe of the calling method is wiped out on return.

The best thing to do here is to simply add an AdjustTimeStyle property to
your dialog and check it when your dialog returns.
 
S

sklett

Ref parameters don't work that way. A ref parameter passes a reference to
a variable into the method, but when you store it as a field the actual
value is copied over to that field. It'd be a bad move, logically, to copy
the reference because often times the variable it refers to would be
destroyed when the stackframe of the calling method is wiped out on
return.

The best thing to do here is to simply add an AdjustTimeStyle property to
your dialog and check it when your dialog returns.
Thank you for the replay Daniel, I learned something and that is always a
good thing :)
Have a good evening,
Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top