0

I am trying to use Convert function from IValueConverter, but I have to call another function in it. I will use his return value but I got that error telling me to return an object value in the converter, any idea how can I avoid this please.

public void Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
    RestClient client = new RestClient();
    client.BaseUrl = "http://";

    RestRequest request = new RestRequest();
    request.Method = Method.GET;
    request.AddParameter("action", "REE");
    request.AddParameter("atm_longitude", location.Longitude);

    client.ExecuteAsync(request, ParseFeedCallBack_ListDistance);
}
public void ParseFeedCallBack_ListDistance(IRestResponse response)
{
    if (response.StatusCode == HttpStatusCode.OK)
    {
        ParseXMLFeedDistance(response.Content);
    }
}
private string ParseXMLFeedDistance(string feed)
{
.... return myvalueToBind;

}

Toni Petrina
  • 7,014
  • 1
  • 25
  • 34
  • The Interface "System.Windows.Data.IValueConverter" expects you to provide implementation for two methods, Convert and ConvertBack, both requires you to return an object. A void method is not valid. You have to at least return null and change your convert method to "public object Convert". As a side note, what exactly are you trying to achieve? Your use of a converter in this case seems like the wrong approach, I obviously can't judge as you haven't provided a context into the general problem. – FunksMaName Feb 26 '14 at 11:18
  • the fact is that i need to use lattitude and longitude foreach item in my listbox and use them to call a webservice to get distance betwen the device and that item..so i used that approach, do you have any other suggestions? – Meriem Ben Mrad Feb 26 '14 at 13:45
  • In which case you're better of doing the calculation locally. You have the device coordinates, and a reference coordinates. Saves you from opening x amount of HTTP connections for every item in the list, saving the device battery and data usage. See response below – FunksMaName Feb 26 '14 at 15:00

1 Answers1

0

A simple way to calculate the distance between two coordinates, in this case, assuming you have the coordinates of the device,

using System.Device.Location;

public class GeoCalculator
{
    public static double Distance(double deviceLongitude, double deviceLatitude, double atmLongitude, double atmLatitude)
    {
        //Coordinates of ATM (or origin).
        var atmCoordinates = new GeoCoordinate(atmLatitude, atmLongitude);

        //Coordinates of Device (or destination).
        var deviceCordinates = new GeoCoordinate(deviceLatitude, deviceLongitude);

        //Distance in meters.
        return atmCoordinates.GetDistanceTo(deviceCordinates);
    }
}

Hence your converter can look like:

public class DistanceConverter : IValueConverter
{
    /// <summary>
    /// This is your device coordinate.
    /// </summary>
    private static GeoCoordinate devCoordinate = new GeoCoordinate(61.1631, -149.9721);

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var location = value as LocationModel;

        if (location != null)
        {
            return GeoCalculator.Distance(devCoordinate.Longitude, devCoordinate.Latitude, location.Longitude, location.Latitude);
        }

        return 0;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

Keep in mind that I would personally not use a converter for this. I would simply expose a simple property in my model that does this calculation as it's a simple logic. If you happen to be a purist and don't like any logic in your model, looping through the list and setting a property on your model would work too.

FunksMaName
  • 2,101
  • 1
  • 15
  • 17
  • this works fine but i'm using a webservice in which i pass the loction of atms and the location device and then i got a result to return it like i explained in my code earlier – Meriem Ben Mrad Feb 26 '14 at 16:45
  • any ideas how can i avoid the return function of parsing to get it in the convert method please? – Meriem Ben Mrad Feb 27 '14 at 08:35