- Joined
- Jan 13, 2008
- Messages
- 2,302
- Reaction score
- 6
- Points
- 38
- Location
- Atlanta, GA, USA, North America
I'm working on a C# program outside of Orbiter, and I've hit a little snag. I need to pass a dictionary from one class (a windows form) into another (call it a child form, though there's no polymorphic relationship) with the result being that I'm able to edit the original dictionary. In C++ this is a no brainer, just pass a pointer to the object to the class, but I'm obviously missing something with C#. Here's my setup, first is the parent class.
Now the windows form I'm opening:
The problem is that when I click the Done button on the child form, dictBlocks is back to null. Does anyone know how to handle this? I need the child form to send data back to the parent since the child form just sort of serves as a way to conveniently enter the data.
Thanks for any help!
Matt
Code:
public partial class Generator : Form
{
private Dictionary<uint, Block> dictBlocks;
public Generator()
{
InitializeComponent();
dictBlocks = new Dictionary<uint, Block>();
}
...
private void btnBlockAdd_Click(object sender, EventArgs e)
{
BlockProperties BlockPropertyDiag = new BlockProperties(ref dictBlocks);
new BlockProperties().ShowDialog();
}
}
Code:
public partial class BlockProperties : Form
{
private Dictionary<uint, Block> dictBlocks;
public BlockProperties(ref Dictionary<uint, Block> dictPassBlocks)
{
InitializeComponent();
this.dictBlocks = dictPassBlocks;
}
...
private void btnDone_Click(object sender, EventArgs e)
{
uint iBlockNum;
if(uint.TryParse(txtBlockNum.Text, out iBlockNum))
{
Block b = new Block();
dictBlocks.Add(iBlockNum, b);
this.Close();
}
else
{
MessageBox.Show("Invalid Block Number", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, 0, false);
txtBlockNum.Clear();
}
}
}
Thanks for any help!
Matt