5

I most be missing something here,
see in the image i made of my debug session.

(items[i] is MailItem) is FALSE according the debugger, but still it enters the if statement.
What am I missing here ?
.

enter image description here

For reference, here is the full code of this method

private MailItem GetMailBySubject(DateTime dateReceived, string subject)
{
    MailItem Result = null;

    Microsoft.Office.Interop.Outlook.Application OutlookIns = new Microsoft.Office.Interop.Outlook.Application();
    Microsoft.Office.Interop.Outlook.NameSpace olNamespace = OutlookIns.GetNamespace("MAPI");
    MAPIFolder myInbox = olNamespace.GetDefaultFolder(OlDefaultFolders.olFolderInbox);


    Items items = myInbox.Items;
    int count = items.Count;
    MailItem mail = null;
    int i = 1; //DO NOT START ON 0

    while ((i < count) && (Result == null))
    {
        if (items[i] is MailItem)
        {
            mail = (MailItem)items[i];
            if ((mail.ReceivedTime.ToString("yyyyMMdd hh:mm:ss") == dateReceived.ToString("yyyyMMdd hh:mm:ss")) && (mail.Subject == subject))
            {
                Result = mail;
            }
        }
        i++;
    }

    return Result;
}
GuidoG
  • 11,359
  • 6
  • 44
  • 79
  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/192756/discussion-on-question-by-guidog-why-does-the-is-operator-enters-an-if-statement). –  May 03 '19 at 08:05
  • Is your program multithreaded? – Eric Lippert May 03 '19 at 13:08
  • @EricLippert no its not – GuidoG May 03 '19 at 13:43
  • I haven't tested this at all, but it might be a RCW/marshaling issue. See the [Runtime Callable Wrapper](https://learn.microsoft.com/en-us/dotnet/framework/interop/runtime-callable-wrapper) structure definition and COM Wrappers in general. – Jimi May 03 '19 at 15:13

2 Answers2

1

This SO answer explains a bit about why you are seeing the IF condition getting passed even when the expression inside it is false. Apparently it's an issue with the debugger and multiple threads. Also, it suggests a workaround to prevent this issue by using lock. Hope it helps.

akg179
  • 1,287
  • 1
  • 6
  • 14
  • Thanks for the suggestion but there is no real working answer in that link. I dont think this applies here – GuidoG May 03 '19 at 13:47
0

I made a workaround using the link provided by Wai Ha Lee. I had to alter it though, because testing if an item is MailItem still behaved strangely.

So I copy the items first into a separate list, and make sure only items of type MailItem are in that list.
The only way I got this filtered is by using try...catch, I would still like a better way and I am still curious why the test if (items[i] is MailItem) behaves so strangely.

List<MailItem> ReceivedEmail = new List<MailItem>();
foreach (var testMail in items)
{
    try
    {
        ReceivedEmail.Add((MailItem)testMail);
    }
    catch (System.Exception ex)
    {
        ;
    }
}

After this I can use the list ReceivedEmail without the check on MailItem.

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
GuidoG
  • 11,359
  • 6
  • 44
  • 79