C&C++   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了C#Strange Index Out Of Bounds异常大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
调试我正在为MS Outlook 2010开发的插件时,我遇到一个奇怪的问题,就是Index Out of Bounds异常.我有一个类进行消息处理,在该类的构造函数中,我传递了@L_577_1@mailItem.然后,我打算运行MailItem的“收件人”列表,并查找在“收件人”,“抄送”和“密件抄送”字段中注册的所有收件人.为此,我有以下代码

public messageProcessor(Outlook.MailItem theMail)
{
  _activeMailItem = theMail;
  _activeMailDetails.Sender = theMail.SenderEmailAddress;
  if (_activeMailItem.Recipients.Count > 0)
  {
    List<String> recipients = new List<String>();
    List<String> cc = new List<String>();
    List<String> bcc = new List<String>();
    for (int i = 0; i < _activeMailItem.Recipients.Count; i++)
    {
      switch (_activeMailItem.Recipients[i].TypE)    <----- HERE
      {
        case (int)Outlook.olMailRecipientType.olTo:
           recipients.Add(_activeMailItem.Recipients[i].Address);
           break;
        case (int)Outlook.olMailRecipientType.olCC:
           cc.Add(_activeMailItem.Recipients[i].Address);
           break;
        case (int)Outlook.olMailRecipientType.olBCC:
           bcc.Add(_activeMailItem.Recipients[i].Address);
           break;
      }
    }
  }
}

但是,我在标有“HERE”的点上得到了例外.当我调试并查看Recipients.Count属性的值时,它显示1.但是,当“i”索引为0(应该是有效索引 – 并且在这种情况下,唯一有效的索引)时会出现问题).当我尝试查看_activeMailItem.Recipients集合时,我看到Count为1;然而,当我试图在结构中进一步向下追踪时,我看到一些红色十字架,我无法检查下面的值.

有谁知道什么可能是错的?

提前致谢.

解决方法

@H_502_22@ Outlook中的所有集合(包括收件人)都是基于1而不是0:

for (int i = 1; i <= _activeMailItem.Recipients.Count; i++)

大佬总结

以上是大佬教程为你收集整理的C#Strange Index Out Of Bounds异常全部内容,希望文章能够帮你解决C#Strange Index Out Of Bounds异常所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。