Click here to Skip to main content
15,879,348 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
hai all,


i am trying to get arraylist form web service and trying to store it in new arraylist but it gives me the following error

following is the code i have used

display.MathService mymathservice = new display.MathService();
        object temp_result = mymathservice.display_messages(this.txtmessage.Text,"velu");
        final_result.Add(temp_result);
           for (int i = 0; i < final_result.Count; i++)
            {
                this.txtareamsg.Value += final_result[i].ToString();
            }


The error message says:
"Unable to cast object of type 'System.Object[]' to type 'System.Collections.ArrayList'."

Now tell me how should i retrieve value from an arraylist but rite now it gives me only system.object..i dont know how to find out index value exactly


please help me
thanks in advance
Posted
Updated 26-Apr-12 21:13pm
v6
Comments
hitech_s 27-Apr-12 2:00am    
at what line you are getting error ?
Iam Velu 27-Apr-12 2:03am    
now look at the questions i made changes ..i want to know how to retrieve object value from an arraylist
m@dhu 27-Apr-12 2:13am    
I didn't get the purpose of using foreach loop in your code as that is not used.
Iam Velu 27-Apr-12 2:21am    
i am just trying boss if u have solution for how to retrieve object value from an arraylist please put the sample code here atleast give me some idea how to execute tis successfully

hai all thank you i have solved this myself..thanks for the replies


C#
this.final_result = new ArrayList();
        livechat.MathService mymath = new livechat.MathService();
        object[] temp = mymath.show_initial_msgs();
        foreach (object o in temp)
        {
            final_result.Add(o.ToString());
        }
        if (final_result.Count == 0)
        {
         //do nothing
        }
        else
        {
            for (int i = 0; i < final_result.Count; i++)
            {
                this.txtareamsg.Value = final_result[i].ToString() + Environment.NewLine;
            }
        }
 
Share this answer
 
v2
Comments
lukeer 27-Apr-12 6:47am    
Then please improve this solution and show us how you made it work. Perhaps someone will read this having a similar problem. And I'm nosey.
You declare temp_result as of type object.
final_result.Add() accepts parameters of type System.Collections.Arraylist. The latter cannot implicitly be converted to the former.

If you know that display_messages() does return an ArrayList, then you should declare temp_result as such in the first place.
If it does not return an ArrayList, then it won't work at all.
 
Share this answer
 
Comments
Iam Velu 27-Apr-12 2:53am    
thanks for ur reply but i have even tried that way if i assign result like this also

final_result = mymathservice.display_messages(this.txtmessage.Text, "velu") as ArrayList;
or
final_result = (ArrayList) mymathservice.display_messages(this.txtmessage.Text, "velu");

then it shows type casting error

Error 1 Cannot convert type 'object[]' to 'System.Collections.ArrayList' via a reference conversion, boxing conversion, unboxing conversion, wrapping conversion, or null type conversion
lukeer 27-Apr-12 4:45am    
That means that you cannot simply cast display_messages()'s return type to ArrayList.
Check the documentation. What type does display_messages() return?
Iam Velu 27-Apr-12 4:59am    
i understand that..display messages return only arraylist i know that..i think the problem is this line i am creating object for calling webservices

display1.MathService mymathservice = new display1.MathService();

now with that object only i am calling display messages() method thatsy it shows error.is there any way to come out from this?please help me iam struck here
Iam Velu 27-Apr-12 5:00am    
i am getting a build error so iam not even able to run the project and see what dsiplaymessages() is returning but i know from web services i am returning only arraylist

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