The syntax for foreach
is
foreach(TYPE item in collection)
{
}
Whereas you have currently
foreach(TYPE item in ANOTHER_TYPE collection)
{
}
So you need something like
foreach (var item in objItems)
{
}
题
I was working on a way to programmatic delete old outlook calendar appointments. I have this code, but I am getting a compile error of 'Invalid Expression Term ')' but it looks A-Okay to me? It's the foreach line that is giving me the trouble.
string strCurrent;
Outlook.Application objApp;
Outlook.NameSpace objNS;
Outlook.MAPIFolder objFolder;
Outlook.Items objItems;
object objItem;
string strStart;
string strEnd;
string strFilter;
strStart = "04/01/2014";
strEnd = "04/28/2014";
strFilter = "[Start] >= " + strStart + " AND [Start] < " + strEnd;
objApp = new Outlook.Application();
objNS = objApp.GetNamespace("MAPI");
objFolder = objNS.GetDefaultFolder(olFolderCalendar);
objItems = objFolder.Items.Restrict(strFilter);
objItem = objItems.GetFirst;
if (objItem = null) { break; }
foreach (object objItem in Outlook.Items objItems) { objItem.Delete; }
objApp = null;
objNS = null;
objFolder = null;
objItems = null;
objItem = null;
解决方案
The syntax for foreach
is
foreach(TYPE item in collection)
{
}
Whereas you have currently
foreach(TYPE item in ANOTHER_TYPE collection)
{
}
So you need something like
foreach (var item in objItems)
{
}
其他提示
the foreach
is not in the correct format.
you wrote:
foreach (object objItem in Outlook.Items objItems) { objItem.Delete; }
it should be:
foreach (object objItem in objItems) { objItem.Delete; }
other than that there is a break
in a context where it is not expected:
if (objItem = null) { break; }
also i suggest a cycle with a backward counter instead of a foreach; when you delete an item the collection is changed and that will create issues at runtime.