Search code examples
c#classoopaccess-levels

class property inaccesible due to protection level


here are my class declarations

public class PlacesAPIResponse
{
    public string formatted_address { get; set; }
    public Geometry geometry { get; set; }
    public Location location { get; set; }
    public string icon { get; set; }
    public Guid id { get; set; }
    public string name { get; set; }
    public string reference { get; set; }
    public string[] types { get; set; }
}
public class Geometry
{
    Location location { get; set; }
}
public class Location
{
    public double lat { get; set; }
    public double lon { get; set; }
}

when i try to access it like this, but i get an "inaccessible due to protection level"

PlacesAPIResponse response = new PlacesAPIResponse();
string lon = response.geometry.location.lon;

what could i be doing wrong ?


Solution

  • Make location public. It is private by default.

    public class Geometry
    {
        public Location location;
    }
    

    And you can access lon as double

    PlacesAPIResponse response = new PlacesAPIResponse();
    double lon = response.geometry.location.lon;