Click here to Skip to main content
15,913,055 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,I want to return an arraylist element using return in c#,But error is 'cannot implicitly convert type object to string'.My code is as...
C#
protected String FindTargetMember(ArrayList MemberList,String TargetMember)
    {
        for (int i = 0; i < MemberList.Count; i++)
        {
            if (TargetMember.Equals(MemberList[i]))
            {
                return MemberList[i];
            }
        }
        return 0;
    }
Posted

Quote:
cannot implicitly convert type object to string
That's because MemberList[i] is an object not a string.
 
Share this answer
 
Comments
SVT02 21-Feb-14 4:55am    
So how to return object in String return type
C#
protected List<string> FindTargetMember(ArrayList MemberList,String TargetMember)
    {
        List<string> list = new List<string>();
        for (int i = 0; i < MemberList.Count; i++)
        {
            if (TargetMember.Equals(MemberList[i]))
            {
               list.Add(MemberList[i].Name.ToString());
// Assuming Name property is there in MemberList objeect. If it's not you can modify the code by yourself.
            }
        }
        return list;
}

-KR
 
Share this answer
 
v2
Since ArrayList is a collection of objects, you have to cast the item to a string (you could just return the input string).
However, please note: it makes more sense to return the index of the found item (or -1 is it is not found).
 
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