Anybody knows what problem has this C# code

J

JC

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)oItems;

//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)oItems;

//Do something with oContact

oContact = null;

}
 
D

Dustin Campbell

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)oItems;

//Do something with oContact

oContact = null;

}

Best Regards,
Dustin Campbell
Developer Express Inc.
 
W

Willy Denoyette [MVP]

|> 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)oItems;
|
| //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.
 
D

Dustin Campbell

You are confusing VB with COM, what is returned is a safearray, and
these are 0 based per default.

I was under the impression that Office interop shared the same issues that
Visual Studio automation interop does (since VS's automation model is based
on Office's). When interoperating with Visual Studio, all collections are
1-based -- even when accessed in C# code due to old VBA support.

Best Regards,
Dustin Campbell
Developer Express Inc.
 
M

Marc Gravell

Either way... it should probably either be 1 ... <= x, or 0 ... < x.

I think we can agree that 0...<=x has a code smell, unless it has a comment
explaining it!

Marc
 
D

Dustin Campbell

Either way... it should probably either be 1 ... <= x, or 0 ... < x.
I think we can agree that 0...<=x has a code smell, unless it has a
comment explaining it!

Yes, definitely! :-D

Best Regards,
Dustin Campbell
Developer Express Inc.
 
J

JC

Sorry, but the problem is not the index for "for" (the problem is not value
for x ). Remember that problem is when x value is 86 (for example) and items
index is valid between 0 and 350 in the test case
Please details the second case too, and this other:
Thanks to Dustin Campbell

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.MAPIFolder aContacts =
olNs.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderContacts);

Microsoft.Office.Interop.Outlook.Items oItems = aContacts.Items;

FAddressBookName = aContacts.AddressBookName;

foreach (Microsoft.Office.Interop.Outlook._ContactItem oContact in oItems)

{

//do something

}
 
J

JC

thanks to everybody.

The problem was simple, very simple, the 86's item was not a contact, is a
list of contact , and it not support the interface!!!

thanks
 
W

Willy Denoyette [MVP]

| 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)
|

This for loop is wrong, 1) here did you get x from? 2)You don't increment i
(at least not in incomplete the code you posted). What's the purpose of
oItems.Count here?
IMO it should look like:

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



Willy.
 
W

Willy Denoyette [MVP]

|> You are confusing VB with COM, what is returned is a safearray, and
| > these are 0 based per default.
|
| I was under the impression that Office interop shared the same issues that
| Visual Studio automation interop does (since VS's automation model is
based
| on Office's). When interoperating with Visual Studio, all collections are
| 1-based -- even when accessed in C# code due to old VBA support.
|
| Best Regards,
| Dustin Campbell
| Developer Express Inc.
|
|
Sorry, You are right, I'm wrong wrong wrong...

Willy.
 
W

Willy Denoyette [MVP]

|
| || 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)
||
|
| This for loop is wrong, 1) here did you get x from? 2)You don't increment
i
| (at least not in incomplete the code you posted). What's the purpose of
| oItems.Count here?
| IMO it should look like:
|
| for (int i = 1; i < oItems.Count; i++)
| ...
|
|
|
| Willy.
|

Sorry bad day. Should be:

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

VARIANT arrays are 1 based the lower limit is 1 the upper limit is the Count
value.Anyway I prefer

foreach (_ContactItem ci in oItems) {
// use ci...
}

Willy.



Willy.
 
J

JC

Thanks Willy.

My problem was the cast, because the list has a list as item. For this
reason the cast: Item to Contact not work!!!

The problem with for loop and control variable (i) was my mistake posted.

Thank!!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top