Return Class data

Jassim Al Rahma 1,526 Reputation points
2022-10-06T05:52:14.837+00:00

Hi,

I have below class to get the current location:

public class GetCurrentLocation  
{  
    public async Task GetLocationDetails()  
    {  
        try  
        {  
            var request = new GeolocationRequest(GeolocationAccuracy.Best, TimeSpan.FromSeconds(10));  
  
            var location = await Geolocation.GetLastKnownLocationAsync();  
  
            if (location == null)  
            {  
                location = await Geolocation.GetLocationAsync(request);  
            }  
  
            if (location != null)  
            {  
                var placemarks = await Geocoding.GetPlacemarksAsync(location.Latitude, location.Longitude);  
                var placemark = placemarks?.FirstOrDefault();  
  
                if (placemark != null)  
                {  
                    RETURN location.Latitude;  
                    RETURN  location.Longitude;  
  
                    RETURN  placemark.CountryCode;  
                    RETURN  placemark.CountryName;  
                }  
            }  
        }  
        catch (Exception ex)  
        {  
            SentrySdk.CaptureException(ex);  
        }  
    }  
}  

How can I call this class in my app to get the RETURNed data in one line like this:

MyLocationData location = GetCurrentLocation();  

and then I can get individual details:

LabelCouuntryCode.Text = location.country_code;  
LabelCouuntryName.Text = location.country_name;  

Thanks,
Jassim

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
2,978 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,390 questions
0 comments No comments
{count} votes

Accepted answer
  1. Karen Payne MVP 35,201 Reputation points
    2022-10-06T08:59:12.247+00:00

    The following is done in a console app, only difference is I print out data to the console rather than set label text.

    And since I don't have the code for geo location and the code for the catch I simply mocked things up.

    The return type of GetLocationDetails is a named value tuple which we deconstruct by the caller.

    internal class Program  
    {  
        static async Task Main(string[] args)  
        {  
            var (latitude, longitude, countryCode, countryName, exception)   
                = await GetCurrentLocation.GetLocationDetails();  
      
            if (exception is null)  
            {  
                Console.WriteLine($"{latitude.Value}, {longitude}, {countryCode}, {countryName}");  
            }  
            else  
            {  
                Console.WriteLine(exception.Message);  
            }  
              
      
        }  
    }  
      
    public class GetCurrentLocation  
    {  
        public static async Task<(decimal? latitude, decimal? longitude, string countryCode, string countryName, Exception exception)> GetLocationDetails()  
        {  
            try  
            {  
                await Task.Delay(0);  
                return (45.103760m, -123.984375m, "en", "usa", null);  
            }  
            catch (Exception ex)  
            {      
                return (null, null, null, null, ex);  
                throw;  
            }  
        }  
    }  
    

1 additional answer

Sort by: Most helpful
  1. Olaf Helper 41,411 Reputation points
    2022-10-06T05:59:45.647+00:00

    I have below class to get the current location:

    Yes, it's a class, but of type Task and a task don't return a result.
    See Task Class => Remarks: The Task class represents a single operation that does not return a value

    0 comments No comments