H
Helmut Giese
Hello out there,
this is a follow-up to a thread from a couple of days ago
Can a 'ref' parameter be saved for later use?
Sorry for the delay - I had to handle a little catastrophe - but still
wanted to continue this discussion.
@Scott M.: Thanks for suggesting to use a Dictionary - makes for much
cleaner code.
@ Christoph Basedau![Smile :) :)](/styles/default/custom/smilies/smile.gif)
To sum up the issue:
In the main form I manage 2 dictionaries. To give the user the
possibility to change some values I open another form. I pass the
appropriate dict and when the user is done editing he closes the form
and the data is passed back to the main form.
Works like it should, but my question concerned 'coupling'.
- In C++ I would pass a pointer to the respective dictionary and the
callee could just update it - no need to know anything about the
caller.
- In the code below, the 'secondary' form
-- receives as one parameter the 'main' form and
-- in order to udate the (possibly modified) data it also has to
know the main form's variable names.
No big deal, just curiosity and wanting to get better aquainted with
C#: How would you implement this scenario?
I could of course post the whole code but the designer generated stuff
is rather large - hopefully the "functional part" below is sufficient
to illustrate the issue.
Thanks for any ideas and best regards
Helmut Giese
--- Code from the 'main' form ---
public partial class MainForm : Form {
// 2 Dictionaries: parameters and signals
public Dictionary<string, string> paramNames, signalNames;
public MainForm() {
InitializeComponent();
// create 2 dictionaries and fill them
paramNames = new Dictionary<string, string>();
signalNames = new Dictionary<string, string>();
for (int i = 1; i <= 4; i++) {
paramNames.Add("Param" + i, "<undefined>");
signalNames.Add("Signal" + i, "<undefined>");
}
}
// 2 event handlers: for parameters and for signals
private void btnParams_Click(object sender, EventArgs e) {
new Assign(paramNames, true, this);
}
private void btnSignals_Click(object sender, EventArgs e) {
new Assign(signalNames, false, this);
}
}
--- Code from the 'secondary' form ---
public partial class Assign : Form {
MainForm mainForm; // 'back link'
bool isParam; // true: handling parameters, else signals
public Assign(Dictionary<string, string>names, bool isParam,
MainForm caller) {
InitializeComponent();
mainForm = caller;
this.isParam = isParam;
// fill the lists
foreach(KeyValuePair<string, string> pair in names) {
listKeys.Items.Add(new ListViewItem(pair.Key));
listValues.Items.Add(new ListViewItem(pair.Value));
}
Text = isParam ? "Define Parameters" : "Define Signals";
this.Show();
}
// User said 'Ok': Pass data back to 'main'
private void btnOk_Click(object sender, EventArgs e) {
Dictionary<string, string> res = new Dictionary<string,
string>();
// Get data from the ListViews
for (int i = 0; i < listKeys.Items.Count; i++)
res.Add(listKeys.Items.Text, listValues.Items.Text);
// ... and update the correct variable in 'main'
if (isParam)
mainForm.paramNames = res;
else
mainForm.signalNames = res;
Close();
}
}
this is a follow-up to a thread from a couple of days ago
Can a 'ref' parameter be saved for later use?
Sorry for the delay - I had to handle a little catastrophe - but still
wanted to continue this discussion.
@Scott M.: Thanks for suggesting to use a Dictionary - makes for much
cleaner code.
@ Christoph Basedau
I didn't see this.Seems some of these advanced C++-skills tend to obstruct
a clear view on the simple and orderly .NET world ;-)
![Smile :) :)](/styles/default/custom/smilies/smile.gif)
To sum up the issue:
In the main form I manage 2 dictionaries. To give the user the
possibility to change some values I open another form. I pass the
appropriate dict and when the user is done editing he closes the form
and the data is passed back to the main form.
Works like it should, but my question concerned 'coupling'.
- In C++ I would pass a pointer to the respective dictionary and the
callee could just update it - no need to know anything about the
caller.
- In the code below, the 'secondary' form
-- receives as one parameter the 'main' form and
-- in order to udate the (possibly modified) data it also has to
know the main form's variable names.
No big deal, just curiosity and wanting to get better aquainted with
C#: How would you implement this scenario?
I could of course post the whole code but the designer generated stuff
is rather large - hopefully the "functional part" below is sufficient
to illustrate the issue.
Thanks for any ideas and best regards
Helmut Giese
--- Code from the 'main' form ---
public partial class MainForm : Form {
// 2 Dictionaries: parameters and signals
public Dictionary<string, string> paramNames, signalNames;
public MainForm() {
InitializeComponent();
// create 2 dictionaries and fill them
paramNames = new Dictionary<string, string>();
signalNames = new Dictionary<string, string>();
for (int i = 1; i <= 4; i++) {
paramNames.Add("Param" + i, "<undefined>");
signalNames.Add("Signal" + i, "<undefined>");
}
}
// 2 event handlers: for parameters and for signals
private void btnParams_Click(object sender, EventArgs e) {
new Assign(paramNames, true, this);
}
private void btnSignals_Click(object sender, EventArgs e) {
new Assign(signalNames, false, this);
}
}
--- Code from the 'secondary' form ---
public partial class Assign : Form {
MainForm mainForm; // 'back link'
bool isParam; // true: handling parameters, else signals
public Assign(Dictionary<string, string>names, bool isParam,
MainForm caller) {
InitializeComponent();
mainForm = caller;
this.isParam = isParam;
// fill the lists
foreach(KeyValuePair<string, string> pair in names) {
listKeys.Items.Add(new ListViewItem(pair.Key));
listValues.Items.Add(new ListViewItem(pair.Value));
}
Text = isParam ? "Define Parameters" : "Define Signals";
this.Show();
}
// User said 'Ok': Pass data back to 'main'
private void btnOk_Click(object sender, EventArgs e) {
Dictionary<string, string> res = new Dictionary<string,
string>();
// Get data from the ListViews
for (int i = 0; i < listKeys.Items.Count; i++)
res.Add(listKeys.Items.Text, listValues.Items.Text);
// ... and update the correct variable in 'main'
if (isParam)
mainForm.paramNames = res;
else
mainForm.signalNames = res;
Close();
}
}