1

I would like to retrieve a list of objects from my web service ,but I am struggling with Object Types.

I have this code :

 [WebMethod]
    public Car[] GetAllCars()
    {

        List<Car> cars = new List<Car>();
        cars.Add(new Car ("fiat", 1999);
        cars.Add("opel" ,2007);
        cars.Add("chevrolet",2007);
        return cars.ToArray(); // 
     }

When I test the web service from my browser ,everything's fine . it displays me what is should.

But in the client side when I try to do

 MyWebService.WebService1SoapClient cars = new MyWebService.WebService1SoapClient();
        Car[] l = (Car[]) cars.GetAllCars();

it says cannot convert ClientApp.MyWebService.Car[] into ClientApp.model.Car[]

the Car class is the same for the both sides ( client and web service).

What should I do to tackle this problem ?

thank you advance .

2
  • try it without convert like this Car[] l =cars.GetAllCars(); Commented Apr 25, 2012 at 1:16
  • It's the first thing I tried to do with no success.Thanks Commented Apr 26, 2012 at 0:11

3 Answers 3

3

While they may look the same, there are two distinct Car classes involved here:

  1. ClientApp.model.Car - This is the original class, hidden behind the webservice.
  2. ClientApp.MyWebService.Car - This is a near copy, created from the SOAP WSDL

The copy will not have any private members, nor any methods that were part of the original Car.

Simply do this to retrieve the cars, being

var carsWebservice = new MyWebService.WebService1SoapClient();
var cars = carsWebservice.GetAllCars();

This will return an array of ClientApp.MyWebService.Car

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your help.If I cannot call any method of my object ,this web service thing is useless.If I need to call ToString() ,how should I proceed ?Thank you
2

If the Car is the same on both sides, then there is no reason to cast it and no reason to use a type on the declaration. Let the compiler decide what type to use for l...

var l = cars.GetAllCars();

Comments

0

It should work for you:

MyWebService.WebService1SoapClient _cars = new MyWebService.WebService1SoapClient();

public List<Car> Cars;

And in the button event for example :

private void button_valider_Click(object sender, EventArgs e)
    {
        SqlCeConnection connexion =Connexion.getInstance().OpenConnection();
        Cars= new List<Car>();
        Cars= _cars .GetAllCars().ToList();

        foreach (var car in Cars)
        {
            int year= car.year;
             ...

        }

    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.