473,320 Members | 1,845 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Anybody knows what problem has this C# code

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

}
Nov 15 '06 #1
12 1907
C# loops are almost always "< x", not "<= x", since they are 0 based.

Marc
Nov 15 '06 #2
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.
Nov 15 '06 #3

"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.
Nov 15 '06 #4
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.
Nov 15 '06 #5
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
Nov 15 '06 #6
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.
Nov 15 '06 #7
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.Out look.OlDefaultFolders.olFolderContacts);

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

FAddressBookName = aContacts.AddressBookName;

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

{

//do something

}

"JC" <jc***@macomexpress.comwrote in message
news:uX**************@TK2MSFTNGP02.phx.gbl...
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;

}


Nov 15 '06 #8
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

"JC" <jc***@macomexpress.comwrote in message
news:uX**************@TK2MSFTNGP02.phx.gbl...
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;

}


Nov 15 '06 #9

"JC" <jc***@macomexpress.comwrote in message
news:uX**************@TK2MSFTNGP02.phx.gbl...
| 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.

Nov 15 '06 #10

"Dustin Campbell" <du*****@no-spam-pleasedevexpress.comwrote in message
news:c1**************************@news.microsoft.c om...
|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.
Nov 15 '06 #11

"Willy Denoyette [MVP]" <wi*************@telenet.bewrote in message
news:uL**************@TK2MSFTNGP03.phx.gbl...
|
| "JC" <jc***@macomexpress.comwrote in message
| news:uX**************@TK2MSFTNGP02.phx.gbl...
|| 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.

Nov 15 '06 #12
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!!!


"Willy Denoyette [MVP]" <wi*************@telenet.bewrote in message
news:uL**************@TK2MSFTNGP03.phx.gbl...
>
"JC" <jc***@macomexpress.comwrote in message
news:uX**************@TK2MSFTNGP02.phx.gbl...
| 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.

Nov 15 '06 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Hans Deragon | last post by:
Greetings. The official PyGTK website is down and not responding for a few days. http://www.daa.com.au/~james/pygtk/ Anybody knows why and where/when it will be brought up?
30
by: aurora | last post by:
I have long find the Python default encoding of strict ASCII frustrating. For one thing I prefer to get garbage character than an exception. But the biggest issue is Unicode exception often pop up...
2
by: Steve | last post by:
Hi, I'd like to know if anybody knows NTsendmail and how I can add variable that point to a textarea defined in a HTML? I can run it now but it shows the example text defined in ntsendmail.pl...
4
by: siddurampure | last post by:
PLZ ANYBODY KNOWS WHY ICAP PROTOCOL IS USED?
4
by: Bore Biko | last post by:
Dear, Does anybody knows a C commpiler with <curses> support under a DOS...?? Thanks in advance, Robert..!! robert.bralic@si.t-com.hr
1
by: Jander | last post by:
Hi. Anybody knows how could take all the messages in the forum to store in my computer? Thanks to all. Jander.
2
by: Vic.Dong | last post by:
Hello: Is there anybody how to create hidden folder in public folder or system? I have a any problem. I cannot create hidden folder in public or system folder. Is there anybody who knows...
3
by: DurumDara | last post by:
Hi ! I need to speedup my MD5/SHA1 calculator app that working on filesystem's files. I use the Python standard modules, but I think that it can be faster if I use C, or other module for it. ...
2
by: czi02 | last post by:
does anybody knows how to connect access to visual basic?? I know that this is for access and database but hope that there is someone who could help me just a simple code on how to connect easch...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.