Items in consecutive Selection collections not the same?

  • Thread starter Thread starter SeekerOfTruths
  • Start date Start date
S

SeekerOfTruths

Hello.

I'm trying to write a VSTO add-in (my first venture in C# & VSTO) which will
allow me to construct wrapper objects around emails selected by the user (so
that I can hook into open events etc.) and I've run into an issue.

When a user selects multiple items I get a call into my SelectionChange
event hook for each item selected. On the first call the explorer.Selection
collection contains, one item, on the second call it contains two items, and
so on as one would expect.

Since I only want to create a single wrapped object for each selected item,
I had assumed that if I were to keep a copy of the previous selection and
then compare it with the current selection I would be able to determine
whether an item had been added to or removed from the selection, and identify
the object added or removed.

This doesn't seem to be the case however - if I compare each object in the
previous selection to each object in the current selection, I don't find any
matches, despite the fact that the second selection should (in theory)
contain all the objects in the first selection.

I can only assume that I'm making some sort of stupid mistake (see the code
below), because the idea that it's supposed to be that way (and that on each
selection change event I would have to destroy all my wrapped objects and
then re-create them again) just seems plain wrong.


void explorer_SelectionChange()
{
if (null != _priorSelection)
{
foreach (object oldItem in _priorSelection)
{
foreach (object newItem in _explorer.Selection)
{
if (oldItem.Equals(newItem))
{
System.Diagnostics.Trace.WriteLine("Match FOUND");
}
else
{
System.Diagnostics.Trace.WriteLine("No match FOUND");
}
}
}
}

_priorSelection = _explorer.Selection;
}

Any help in pointing out where I've gone wrong would be greatly appreciated!

SeekerOfTruths.
 
What version of Outlook?

You most likely will not be able to compare the items from the selections
directly using Equals or by comparing hash codes, although you can try hash
codes. Most likely you will have to empty your collection when selection
changes and reconstitute it.
 
Sorry I forgot to mention the Outlook version. It's 2007 SP1.

Thanks for the response though, even if the answer was the one I was hoping
I wouldn't hear!

It really does sound terribly inefficient to have to clear down my
collection of wrapper objects (and undo all their event hooks) each time the
selection changes and then rebuild it again based upon the new selection. If
I select 10 objects by holding down CTRL and clikcing on each object in turn
this will result in 10 calls to on selection change and I will have created
55 wrapper objects in total and destroyed 45 of them.

If the uderyling objects in the selection had stayed the same (perhaps they
are and I just can tell) then I could have just identified the difference
between selections and only created (or destroyed) objects as required.

Oh well, I'll give the hashcodes a go and if that doesn't work then I'll
just have to bite the bullet and go with the clear down and rebuild approach.

Thanks for the assistance though.

SeeketOfTruths.
 
Back
Top