Haider Khattak
Haider Khattak

Reputation: 577

how to add values to a method with return type list

The problem which i am facing is in adding values to the method with return type list. I am sending values from another class. when the i want to enter to values from the array into the method of the other class i gives me the paring error.

here is the code which i am using: (class from where i will send data)

public partial class _Default : System.Web.UI.Page
{
   public static double GetDistance()
    {
        string[] rep = new string[] { "51.48797159999999", "-0.16511850000006234", "51.492172", "-0.16144499999995787"};
        List<getLatLong> lst = new List<getLatLong>();      

        for (int i = 0; i < rep.Length; i++)
        {
            lst.Add(new getLatLong { Lat = rep[i], Lng= rep[i + 1] });  //i am getting the error over here
            i++;
        }
     }
}

class getLatLong is the class where i am going to send and add value to its return type list methods:

public class getLatLong
{
    public getLatLong()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    private List<double> lat;

    public List<double> Lat
    {
        get { return lat; }
        set { lat = value; }
    }

    private List<double> lng;

    public List<double> Lng
    {
        get { return lng; }
        set { lng = value; }
    }
}

Upvotes: 2

Views: 972

Answers (2)

Giri
Giri

Reputation: 126

The code doesn't compile because of incompatible types:

  • In GetDistance() method, rep array should be of double[] type rather than string[] or else you need to convert the types while creating getLatLong objects.
  • In GetDistance() method, while creating getLatLong objects, there is a incompatible type assignment, where Lat and Lng objects are initialized. Either these should be initialized to List objects as shown in code below or the types for Lat and Lng in the getLatLong class should be changed to double instead of List.
public static double GetDistance()
{
   double[] rep = new double[] { 51.4879716, -0.16511850, 51.492172, -0.161445 };
   List<getLatLong> lst = new List<getLatLong>();

   for (int i = 0; i < rep.Length; i++)
   {
        lst.Add(new getLatLong {
            Lat = new List<double> {rep[i]}, 
            Lng = new List<double> {rep[i + 1]}
        });
        i++;
   }

}

Upvotes: 1

drew_w
drew_w

Reputation: 10430

The code won't compile because a String is not a List<double> and the compiler doesn't know how to make that conversion. Technically all you would need to do is initialize your object like:

lst.Add(new getLatLong {
    Lat = new List<double>() {Double.Parse(rep[i])},
    Lng = new List<double>() {Double.Parse(rep[i + 1])}
  }); 

With that the code should compile. That said, there are a number of places where the code doesn't exactly make sense. For example, why is your coordinate class named getLatLong and also why does that class consist of a list of doubles rather than just two double values? You may want to consider a few of these things before just fixing the compiler errors. Either way, best of luck!

NOTE: If you have lots of GEO Coordinate type things to do you might just consider referencing a prebuilt library such as GeoSpatial.

Upvotes: 2

Related Questions