C
Chris
Hi all,
I've recently discovered the joys of tag I have a ListView which is
populated with strings from a List<>, and each ListView item is linked to
the relevant object in List<> using Tag.
The ListView is on a parent form, and I have a child form which adds items
to List<>. So I pass the List<> by ref*, the relevant objects are created in
the List<> and the ListView is updated in the parent form.
I'm now trying to edit items in the ListView. If an item is selected, then
ListView.SelectedItems[0].Tag is used to get the object to edit. I need this
object passed to the child form, repopulating the relevant fields to allow
for editing. I can't seem to pass the object in
ListView.SelectedItems[0].Tag, but I if I create a temp object, assign
ListView.SelectedItems[0].Tag to this, pass this object to the child form,
and assign it back to ListView.SelectedItems[0].Tag it seems to work e.g.:
if (lvMainList.SelectedItems[0].Tag is Journal)
{
Journal temp =
((Journal)lvMainList.SelectedItems[0].Tag);
JournalEntryForm journalEntry
= new JournalEntryForm(ref temp);
lvMainList.SelectedItems[0].Tag = temp;
journalEntry.ShowDialog();
journalEntry.Dispose();
}
Is this an ok way to do this, or is there a more appropriate way? Also, what
happens to the old journal object when I reassign temp to the same Tag? Is
it garbage collected? Or does this waste memory?
Sorry for all the (perhaps obvious) questions! Any help would be greatly
appreciated
Chris
*as the list is ref type, is it passed by ref by anyway? I used the ref
keywords to be explicit - is this overkill?
I've recently discovered the joys of tag I have a ListView which is
populated with strings from a List<>, and each ListView item is linked to
the relevant object in List<> using Tag.
The ListView is on a parent form, and I have a child form which adds items
to List<>. So I pass the List<> by ref*, the relevant objects are created in
the List<> and the ListView is updated in the parent form.
I'm now trying to edit items in the ListView. If an item is selected, then
ListView.SelectedItems[0].Tag is used to get the object to edit. I need this
object passed to the child form, repopulating the relevant fields to allow
for editing. I can't seem to pass the object in
ListView.SelectedItems[0].Tag, but I if I create a temp object, assign
ListView.SelectedItems[0].Tag to this, pass this object to the child form,
and assign it back to ListView.SelectedItems[0].Tag it seems to work e.g.:
if (lvMainList.SelectedItems[0].Tag is Journal)
{
Journal temp =
((Journal)lvMainList.SelectedItems[0].Tag);
JournalEntryForm journalEntry
= new JournalEntryForm(ref temp);
lvMainList.SelectedItems[0].Tag = temp;
journalEntry.ShowDialog();
journalEntry.Dispose();
}
Is this an ok way to do this, or is there a more appropriate way? Also, what
happens to the old journal object when I reassign temp to the same Tag? Is
it garbage collected? Or does this waste memory?
Sorry for all the (perhaps obvious) questions! Any help would be greatly
appreciated
Chris
*as the list is ref type, is it passed by ref by anyway? I used the ref
keywords to be explicit - is this overkill?