Click here to Skip to main content
15,917,176 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
private ArrayList newFiles = new ArrayList();
string filename = "logFile.txt";
int i = 0;


for (i = 0; i < newFiles.Count; i++)
{
if (filename == newFiles[i])
MessageBox.Show("Found " + i);
else
MessageBox.Show("Not Same " + i);
}

i tried to compare a var filename type of string with newFiles type of Arraylist...
it indicated those error..possible unintended string comparison to get a value comparison, cast the right hand side...anyone can help? thanks in advance..
Posted

if(filename == newFiles[i].ToString)

or maybe

if(filename == System.Convert.ToString(newFiles[i])

or cast the right hand side i think like this...

if(filename == (string)newFiles[i])
 
Share this answer
 
Comments
beh7606 9-Dec-11 13:10pm    
hahaha...thanks dallyck!! i owe u...
Use a List<string> rather than an ArrayList.

The problem here is that an ArrayList can contain everything by nature, not just strings. So here, when you try to compare the value of the list at the current index, compiler is not sure this value is actually a string. Hence the problem.

With a List<string> you would not have this issue.

Otherwise, you are creating an empty list at the beginning of your procedure, and then directly start to iterate over it. Maybe you didn't want to bother us with the init procedure, or maybe you forgot it ?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900