Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / November 2006

Tip: Looking for answers? Try searching our database.

Anybody knows what problem has this C# code

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
JC - 15 Nov 2006 14:11 GMT
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[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)oItems[i];

               //Do something with oContact

               oContact = null;

           }
Marc Gravell - 15 Nov 2006 14:53 GMT
C# loops are almost always "< x", not "<= x", since they are 0 based.

Marc
Dustin Campbell - 15 Nov 2006 15:04 GMT
> 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[i];

//Do something with oContact

oContact = null;

}

Best Regards,
Dustin Campbell
Developer Express Inc.
Willy Denoyette [MVP] - 15 Nov 2006 15:20 GMT
|> C# loops are almost always "< x", not "<= x", since they are 0 based.
|
[quoted text clipped - 13 lines]
|
| }

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

Willy.
Dustin Campbell - 15 Nov 2006 15:26 GMT
> 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.
Marc Gravell - 15 Nov 2006 15:55 GMT
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
Dustin Campbell - 15 Nov 2006 16:03 GMT
> 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.
Willy Denoyette [MVP] - 15 Nov 2006 17:50 GMT
|> You are confusing VB with COM, what is returned is a safearray, and
| > these are 0 based per default.
[quoted text clipped - 7 lines]
| Dustin Campbell
| Developer Express Inc.

Sorry, You are right, I'm wrong wrong wrong...

Willy.
JC - 15 Nov 2006 16:37 GMT
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

}

> Hi...
>
[quoted text clipped - 65 lines]
>
>            }
JC - 15 Nov 2006 17:14 GMT
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

> Hi...
>
[quoted text clipped - 65 lines]
>
>            }
Willy Denoyette [MVP] - 15 Nov 2006 17:48 GMT
| Hi...
|
[quoted text clipped - 19 lines]
|
|            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.
Willy Denoyette [MVP] - 15 Nov 2006 17:59 GMT
|| Hi...
||
[quoted text clipped - 32 lines]
|
| 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.
JC - 15 Nov 2006 19:21 GMT
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!!!

> | Hi...
> |
[quoted text clipped - 33 lines]
>
> Willy.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.