मैं फ्रेच लूप में ArrayList से आइटम हटाता हूं और follwing अपवाद प्राप्त करता हूं।संग्रह पर पुनरावृत्ति के दौरान अपवाद और उस संग्रह से आइटम हटाएं
संग्रह संशोधित किया गया था; गणना ऑपरेशन निष्पादित नहीं हो सकता है।
मैं कैसे, foreach में आइटम निकाल सकते हैं
संपादित करें:या दो या सभी को दूर करने के एक आइटम हो सकती है।
/*
* Need to remove all items from 'attachementsFielPath' which does not exist in names array.
*/
try
{
string attachmentFileNames = txtAttachment.Text.Trim(); // Textbox having file names.
string[] names = attachmentFileNames.Split(new char[] { ';' });
int index = 0;
// attachmentsFilePath is ArrayList holding full path of fiels user selected at any time.
foreach (var fullFilePath in attachmentsFilePath)
{
bool isNeedToRemove = true;
// Extract filename from full path.
string fileName = fullFilePath.ToString().Substring(fullFilePath.ToString().LastIndexOf('\\') + 1);
for (int i = 0; i < names.Length; i++)
{
// If filename found in array then no need to check remaining items.
if (fileName.Equals(names[i].Trim()))
{
isNeedToRemove = false;
break;
}
}
// If file not found in names array, remove it.
if (isNeedToRemove)
{
attachmentsFilePath.RemoveAt(index);
isNeedToRemove = true;
}
index++;
}
}
catch (Exception ex)
{
throw ex;
}
संपादित करें:
बादमेरी कोड है कोड पर आप भी कर सकते हैं सलाह। मैं छोटे तरीकों और अपवाद ArrayList
foreach (var fullFilePath in new List<string>(attachmentsFilePath))
{
alt text http://img641.imageshack.us/img641/1628/invalidargument1.png
से सामान्य सूची बनाने की आदि
अमान्य तर्क अपवाद हैंडलिंग में तोड़ने के लिए जब मैं List<ArrayList>
का उपयोग की आवश्यकता है अपवाद तर्क '1': 'System.Collections.ArrayList' से 'int' में परिवर्तित नहीं हो सकता
attachmentsFilePath इस
ArrayList attachmentsFilePath = new ArrayList();
की तरह घोषित किया जाता है लेकिन जब मैं इसे इस तरह की घोषणा की, समस्या हल हो
List<ArrayList> attachmentsFilePath = new List<ArrayList>();
+1, हालांकि 'ArrayList' को उचित रूप से टाइप की गई 'सूची' के साथ प्रतिस्थापित किया जाना चाहिए। –
मैंने नई सूची (attachementsFilePath) बनाई है और यह मुझे अवैध arguemnt अपवाद देता है। मैंने प्रश्न क्षेत्र में Iamge संलग्न किया है। –
Kashif