有人知道这个C#代码有什么问题 [英] Anybody knows what problem has this C# code
问题描述
嗨...
有人知道这段代码有什么问题吗?我想,在垃圾收集器中?
你知道解决方案吗?
测试案例中的程序,350个联系人,运行正常数字86.
错误是数组索引超出范围。
Microsoft.Office.Interop.Outlook._Application olApp = new
Microsoft.Office.Interop.Outlook.ApplicationClass();
Microsoft.Office.Interop.Outlook._NameSpace olNs =
olApp .GetNamespace(" MAPI");
Microsoft.Office.Interop.Outlook._Folders oFolders =
olNs.Folders;
Microsoft.Office.Interop.Outlook.MAPIFolder aContacts =
olNs.PickFolder();
Microsoft.Office.Interop.Outlook .Items oItems = aContacts.Items;
for(int i = 0; i< = x; oItems.Count)
{ // Explota en la proxima linea。
Microsoft.Office.Interop.Outlook._ContactItem oContact =
(Microsoft.Office.Interop.O utlook._ContactItem)o ems [i];
//用oContact做点什么
oContact = null;
}
在第二种情况下,错误出现在for之前的行中。
Microsoft .Office.Interop.Outlook._Application olApp = new
Microsoft.Office.Interop.Outlook.ApplicationClass();
Microsoft.Office.Interop.Outlook ._NameSpace olNs =
olApp.GetNamespace(" MAPI");
Microsoft.Office.Interop.Outlook._Folders oFolders =
olNs.Folders;
Microsoft.Office.Interop.Outlook.MAPIFolder aContacts =
olNs.PickFolder();
Microsoft.Office.Interop.Outlook.Items oItems = aContacts.Items;
int x = oItems.Count;
// Explota en la proxima linea。
for(int i = 0;我< = x; i ++)
{
Microsoft.Office.Interop.Outlook._ContactItem oContact =
(Microsoft.Office .Interop.Outlook._ContactItem)o et [i];
//用oContact做点什么
oContact = null;
}
Hi...
Anybody knows what problem has this code? I think, in the Garbage Collector?
You know the Solution?
The program in the test''s case, whit 350 contacts, run OK before number 86.
The error is a "Array index out of bounds".
Microsoft.Office.Interop.Outlook._Application olApp = new
Microsoft.Office.Interop.Outlook.ApplicationClass( );
Microsoft.Office.Interop.Outlook._NameSpace olNs =
olApp.GetNamespace("MAPI");
Microsoft.Office.Interop.Outlook._Folders oFolders =
olNs.Folders;
Microsoft.Office.Interop.Outlook.MAPIFolder aContacts =
olNs.PickFolder();
Microsoft.Office.Interop.Outlook.Items oItems = aContacts.Items;
for (int i = 0; i <= x; oItems.Count)
{ //Explota en la proxima linea.
Microsoft.Office.Interop.Outlook._ContactItem oContact =
(Microsoft.Office.Interop.Outlook._ContactItem)oIt ems[i];
//Do something with oContact
oContact = null;
}
In this second case, the error appear in the line before the "for".
Microsoft.Office.Interop.Outlook._Application olApp = new
Microsoft.Office.Interop.Outlook.ApplicationClass( );
Microsoft.Office.Interop.Outlook._NameSpace olNs =
olApp.GetNamespace("MAPI");
Microsoft.Office.Interop.Outlook._Folders oFolders =
olNs.Folders;
Microsoft.Office.Interop.Outlook.MAPIFolder aContacts =
olNs.PickFolder();
Microsoft.Office.Interop.Outlook.Items oItems = aContacts.Items;
int x = oItems.Count;
//Explota en la proxima linea.
for (int i = 0; i <= x; i++)
{
Microsoft.Office.Interop.Outlook._ContactItem oContact =
(Microsoft.Office.Interop.Outlook._ContactItem)oIt ems[i];
//Do something with oContact
oContact = null;
}
推荐答案
C#循环几乎总是<< x" not not"< = x",因为它们基于0。
Marc
C# loops are almost always "< x", not "<= x", since they are 0 based.
Marc
C#循环几乎总是<< x",not"< = x",因为它们基于0。
实际上,由于这是与Office交互的代码,它应该可能是
是一个基于1的循环:
int x = oItems.Count;
for(int i = 1; i< ; = x; i ++)
{
Microsoft.Office.Interop.Outlook._ContactItem oContact =(Microsoft.Office.Interop.Outlook._ContactItem)oIt ems [i];
//用oContact做点什么
oContact = null;
}
最诚挚的问候,
Dustin Campbell
Developer Express Inc.
C# loops are almost always "< x", not "<= x", since they are 0 based.
Actually, since this is code that is interoping with Office, it should probably
be a 1-based loop:
int x = oItems.Count;
for (int i = 1; i <= x; i++)
{
Microsoft.Office.Interop.Outlook._ContactItem oContact = (Microsoft.Office.Interop.Outlook._ContactItem)oIt ems[i];
//Do something with oContact
oContact = null;
}
Best Regards,
Dustin Campbell
Developer Express Inc.
>
" Dustin Campbell" < du ***** @ no-spam-pleasedevexpress.com写在留言中
新闻:c1 ********************* *****@news.microsoft.c om ...
| C#循环几乎总是<< x",not"< = x",因为它们基于0。
|
|实际上,由于这是与Office进行交互的代码,它应该是
可能
|是一个基于1的循环:
|
| int x = oItems.Count;
|
| for(int i = 1; i< = x; i ++)
| {
|
| Microsoft.Office.Interop.Outlook._ContactItem oContact =
(Microsoft.Office.Interop.Outlook._ContactItem)o ems [i];
|
| //用oContact做点什么
|
| oContact = null;
|
| }
你把VB与COM混淆了,返回的是一个safearray,这些
默认为0。
威利。
"Dustin Campbell" <du*****@no-spam-pleasedevexpress.comwrote in message
news:c1**************************@news.microsoft.c om...
|C# loops are almost always "< x", not "<= x", since they are 0 based.
|
| Actually, since this is code that is interoping with Office, it should
probably
| be a 1-based loop:
|
| int x = oItems.Count;
|
| for (int i = 1; i <= x; i++)
| {
|
| Microsoft.Office.Interop.Outlook._ContactItem oContact =
(Microsoft.Office.Interop.Outlook._ContactItem)oIt ems[i];
|
| //Do something with oContact
|
| oContact = null;
|
| }
You are confusing VB with COM, what is returned is a safearray, and these
are 0 based per default.
Willy.
这篇关于有人知道这个C#代码有什么问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!