'how do i return this instance of the IInfocard interface?

i am currently trying to return a instance of the Iinforcard interface within my "createNewInfocard" function, however i keep running into error CS0266. i am unsure as to how fix this.

public interface IInfoCard
    {
        string Name { get; set; }
        string Category { get; }
        string GetDataAsString();
        void DisplayData(Panel displayPanel);
        void CloseDisplay();
        bool EditData();
    }




public interface IInfoCardFactory
{
    IInfoCard CreateNewInfoCard(string category);
    IInfoCard CreateInfoCard(string initialDetails);
    string[] CategoriesSupported { get; }
    string GetDescription(string category);
}

 public class Class1 : IInfoCardFactory
    {

    public IInfoCard CreateNewInfoCard(string category)
    {

        Class1 x;
        x = new Class1();
        return x;// i keep at getting error CS0266 at this return statement.
    }


}


Solution 1:[1]

Class1 must implement IInfoCard interface, i.e.

  public class Class1 : 
     IInfoCardFactory, 
     IInfoCard  // <- notice IInfoCard implementation
  {
     public IInfoCard CreateNewInfoCard(string category)
     {
        Class1 x;
        x = new Class1();
        return x;// i keep at getting error CS0266 at this return statement.
     }
     ...
     //TODO: put here IInfoCard methods and properties implementations
  }

now, in your current code, Class1 implements IInfoCardFactory but not IInfoCard

Solution 2:[2]

Do you have a class that implements your IInfoCard interface?

An interface on its own is just going to define some functionality that a class should have, so you need a concrete implementation in order to use it :

// Notice the IInfoCard declaration, indicating Class1 implements it
public class Class1 : IInfoCardFactory, IInfocard
{
     /* Omitted for brevity */
}

This should allow you to return your Class1 object from your CreateNewInfoCard() method as it will now qualify as an IInfoCard object since it implements it.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dmitry Bychenko
Solution 2 Rion Williams