Hi,
What is the best way to set a "out" variable in a WinForm passed by
a ShowDialog caller?
Currently I am setting a public static variable in the ShowDialog
callers' class in the WinForm - I am hoping there is a less ugly way.
Thanks,
Viepia
Harvey - 30 Jan 2008 18:02 GMT
You can easily access a public value once the form is closed that
contains the assignment you make in the form. So let's say you have a
main form:
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
WinForm wf = new WinForm();
if (wf.ShowDialog().Equals(DialogResult.OK))
MessageBox.Show(wf.SomePublicString);
}
}
You can see the wf.SomePublicString is available to you... Here is the
WinForm:
public partial class WinForm : Form
{
public string SomePublicString
{
get { return somePublicString; }
set { somePublicString = value; }
}
private string somePublicString = string.Empty;
public WinForm()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
SomePublicString = "Hello World!";
DialogResult = DialogResult.OK;
}
}
Hope that helps...
On Jan 30, 10:51 am, vie...@nospam.com wrote:
> Hi,
> What is the best way to set a "out" variable in a WinForm passed by
[quoted text clipped - 5 lines]
> Thanks,
> Viepia
Nicholas Paldino [.NET/C# MVP] - 30 Jan 2008 18:03 GMT
Viepia,
Why not expose the value through a property on the instance? Then,
after the call to ShowDialog is made, the caller can access the value
through the property on the instance that ShowDialog was called on.

Signature
- Nicholas Paldino [.NET/C# MVP]
- mvp@spam.guard.caspershouse.com
> Hi,
> What is the best way to set a "out" variable in a WinForm passed by
[quoted text clipped - 5 lines]
> Thanks,
> Viepia
viepia@nospam.com - 31 Jan 2008 07:13 GMT
Thanks for helping my code look clearner. For some dumb reason I
didn't think that the instance data of a closed Winform was valid.
Viepia
>Viepia,
>
> Why not expose the value through a property on the instance? Then,
>after the call to ShowDialog is made, the caller can access the value
>through the property on the instance that ShowDialog was called on.
Ignacio Machin ( .NET/ C# MVP ) - 30 Jan 2008 20:13 GMT
Hi,
A simple public property will do:
MyForm f = new MyForm();
if ( f.ShowDialog() == DialogResult ...)
{
DoSomething( f.MyProperty);
}

Signature
Ignacio Machin
http://www.laceupsolutions.com
Mobile & warehouse Solutions.
> Hi,
> What is the best way to set a "out" variable in a WinForm passed by
[quoted text clipped - 5 lines]
> Thanks,
> Viepia