Passing object between event handlers?
-
Hi all, im new to C# (coming from matlab), and so theres a lot to explore for me (the whole OOP thing ;) ). Currently I write a simple program which deserializes a xml file (on a menu click) and shows the xml structure in a treeview. My Problem: When clicking on an element of the treeview, i want to show the content of the chosen xml field in a datagrid. This means I have to make the deserialized xml (which is an object) avaialable for the treeView1_AfterSelect. Maybe someone has a hint for me? I already searched for a solutions which fits this problem :). Maybe its to obvious ^^? Here's some code (im very new to OOP so dont laugh :D).
...
private void loadObjectsToolStripMenuItem\_Click(object sender, EventArgs e) {
// get xml File
string FilePath = string.Empty;OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFileDialog1.Filter = "xml files (\*.xml)|\*.xml| All files (\*.\*)|\*.\*"; openFileDialog1.InitialDirectory = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(Application.ExecutablePath), @""); if (openFileDialog1.ShowDialog() == DialogResult.OK) FilePath = openFileDialog1.FileName;
// deserialize xml file, so i have an object
xmlDataThings xmlData= (xmlDataThings )xmlReadWrite.DeSerializeFromXML(FilePath, typeof(xmlDataThings ));//create treeview and add nodes (works fine)
TreeNode tNode = new TreeNode(openFileDialog1.SafeFileName);if (treeViewObjects.Nodes.ContainsKey(openFileDialog1.SafeFileName)) return; tNode.Name = openFileDialog1.SafeFileName; treeViewObjects.Nodes.Add(tNode); tNode = treeViewObjects.Nodes\[treeViewObjects.Nodes.Count-1\]; AddNode(xmlData, tNode); treeViewObjects.ExpandAll(); }
// !!!!!!!! Want to have the xmlData in here HELP :D !!!!!!
private void treeViewObjects_AfterSelect(object sender, TreeViewEventArgs e)
{
if (treeViewObjects.SelectedNode.Level == 2)
{// please xmlData, come in here RIGHT HERE!!!!!! }
...
}Would be very nice if someone has a hint for me.
-
Hi all, im new to C# (coming from matlab), and so theres a lot to explore for me (the whole OOP thing ;) ). Currently I write a simple program which deserializes a xml file (on a menu click) and shows the xml structure in a treeview. My Problem: When clicking on an element of the treeview, i want to show the content of the chosen xml field in a datagrid. This means I have to make the deserialized xml (which is an object) avaialable for the treeView1_AfterSelect. Maybe someone has a hint for me? I already searched for a solutions which fits this problem :). Maybe its to obvious ^^? Here's some code (im very new to OOP so dont laugh :D).
...
private void loadObjectsToolStripMenuItem\_Click(object sender, EventArgs e) {
// get xml File
string FilePath = string.Empty;OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFileDialog1.Filter = "xml files (\*.xml)|\*.xml| All files (\*.\*)|\*.\*"; openFileDialog1.InitialDirectory = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(Application.ExecutablePath), @""); if (openFileDialog1.ShowDialog() == DialogResult.OK) FilePath = openFileDialog1.FileName;
// deserialize xml file, so i have an object
xmlDataThings xmlData= (xmlDataThings )xmlReadWrite.DeSerializeFromXML(FilePath, typeof(xmlDataThings ));//create treeview and add nodes (works fine)
TreeNode tNode = new TreeNode(openFileDialog1.SafeFileName);if (treeViewObjects.Nodes.ContainsKey(openFileDialog1.SafeFileName)) return; tNode.Name = openFileDialog1.SafeFileName; treeViewObjects.Nodes.Add(tNode); tNode = treeViewObjects.Nodes\[treeViewObjects.Nodes.Count-1\]; AddNode(xmlData, tNode); treeViewObjects.ExpandAll(); }
// !!!!!!!! Want to have the xmlData in here HELP :D !!!!!!
private void treeViewObjects_AfterSelect(object sender, TreeViewEventArgs e)
{
if (treeViewObjects.SelectedNode.Level == 2)
{// please xmlData, come in here RIGHT HERE!!!!!! }
...
}Would be very nice if someone has a hint for me.
Simplest thing would be to make xmlData a member of the class instead of a local variable. Then you could refer to it from both methods.
-
Simplest thing would be to make xmlData a member of the class instead of a local variable. Then you could refer to it from both methods.
As I said, it was obvious ^^ thank you for the hint. Just needed a static object (that was the point) where I have to "set" the object at first deserialize and afterwards i can "get" it from there...
public class xmlReadWrite { static private object \_xmlData; public object getXmlData() { return \_xmlData; } public void setXmlData(object xmlData) { \_xmlData= xmlData; } }
-
As I said, it was obvious ^^ thank you for the hint. Just needed a static object (that was the point) where I have to "set" the object at first deserialize and afterwards i can "get" it from there...
public class xmlReadWrite { static private object \_xmlData; public object getXmlData() { return \_xmlData; } public void setXmlData(object xmlData) { \_xmlData= xmlData; } }
You should not use the static keyword here. "private object _xmlData = null;" is correct.
-
You should not use the static keyword here. "private object _xmlData = null;" is correct.
this doesnt work (I've tried). Because the "static" is needed so the class doesnt loose the object after the first call (as far as i understand :D)
-
this doesnt work (I've tried). Because the "static" is needed so the class doesnt loose the object after the first call (as far as i understand :D)
It will only lose the object when the containing class is destroyed. From your original post, it seemed like everything is in the same class.