1

I have interface with this code

interface IHttpRequest
{
    Task<string> GetCountries();
}

That I inherit in class like this

public class GettingCountries: IHttpRequest
{
    public async Task<string> GetCountries()
    {
        var client = new RestClient("http://api.xplorpal.com");
        var request = new RestRequest("/countries", Method.POST);
        var response = await client.ExecuteTaskAsync(request);
        var content = response.Content;
        var responseCountries = JsonConvert.DeserializeObject<IEnumerable<GettingCountry.RootObjectCountries>>(content);

        GettingCountry.CountriesList.Clear();
        GettingCountry.ListOfCountriesRoot.Clear();

        foreach (var country in responseCountries)
        {
            GettingCountry.CountriesList.Add(country.nicename);
            GettingCountry.ListOfCountriesRoot.Add(new GettingCountry.RootObjectCountries(country.id, country.nicename));
        }
        return content;
    }


}

After this I need to call method of this class somewhere, like this for example var countries_list = await GettingCountries.GetCountries();

But now I have error.

enter image description here

How I can solve it? I can make it static, but is this ok? Maybe smth other?

Eugene Sukh
  • 2,357
  • 4
  • 42
  • 86
  • 1
    You can create instance object for `GettingCountries` instead of making it static. – SᴇM Jan 16 '19 at 07:07
  • 1
    `await (new GettingCountries()).GetCountries();`? But presumably there's a reason that this is being defined on an interface, so there may be a "better" way of obtaining the "correct" instance rather than `new`ing one up. – Damien_The_Unbeliever Jan 16 '19 at 07:07
  • seems thats it. Thank's @Damien_The_Unbeliever – Eugene Sukh Jan 16 '19 at 07:08
  • 1
    You need to create an instance of `GettingCountries` before calling its methods. (+I'm picky but you don't inherit from an interface, you implement it) – vc 74 Jan 16 '19 at 07:08
  • Yeah. But, I'm inherit interface, and that implement it@vc74 – Eugene Sukh Jan 16 '19 at 07:10
  • Not sure I'm following you. `GettingCountries` implements `IHttpRequest`, it does not inherit from `IHttpRequest`. In other words `IHttpRequest` is an interface, not a parent class. – vc 74 Jan 16 '19 at 07:12
  • @EugeneSukh implementing an interface is very different than inheriting a base class. Read [my answer](https://stackoverflow.com/questions/52843692/inheritance-internal-class-vs-internal-interface/52944818#52944818) to [Inheritance: Internal Class vs. Internal Interface](https://stackoverflow.com/questions/52843692/inheritance-internal-class-vs-internal-interface) and also [What is the difference between inherits and implements in C#](https://stackoverflow.com/questions/12020286/what-is-the-difference-between-inherits-and-implements-in-c-sharp) for more details. – Zohar Peled Jan 16 '19 at 07:17

0 Answers0