3
interface ISample
{
    int fncAdd();
}

class ibaseclass
{
    public int intF = 0, intS = 0;
    int Add()
    {
        return intF + intS;
    }
}
class iChild : ibaseclass, ISample
{
    int fncAdd()
    {
        int intTot = 0;
        ibaseclass obj = new ibaseclass();
        intTot = obj.intF;
        return intTot;
    }
}

I want to call ISample in static void Main(string[] args) but I dont know how to do that. Could you please tell me how?

Venemo
  • 17,191
  • 9
  • 78
  • 117
sam
  • 151
  • 4
  • 5
  • 10
  • 7
    According to your question, I really don't think you have grasped the purpose of an interface. – Øyvind Bråthen Mar 14 '11 at 09:42
  • 1
    As mentioned above, I don't think you know what interfaces are. See this http://msdn.microsoft.com/en-us/library/87d83y5b%28v=vs.80%29.aspx – Fishcake Mar 14 '11 at 09:45

3 Answers3

10

An interface cannot be instantiated by itself. You can't just call an interface. You need to instantiate a class that actually implements the interface.

Interfaces don't and can't do anything by themselves.

For example:

ISample instance = new iChild(); // iChild implements ISample
instance.fncAdd();

The following questions provide more detailed answers about this:

Community
  • 1
  • 1
Venemo
  • 17,191
  • 9
  • 78
  • 117
2

You can't "call" interface or create instance of it.

What you can do is have your class implement the interface then use its method fncAdd.

Shadow The Vaccinated Wizard
  • 62,584
  • 26
  • 129
  • 194
2

Do you mean?:

static void Main(string[] args)
{
    ISample child = new iChild();
    child.fncAdd();
}

Although, as stated by others the code doesn't seem like it's using inheritance correctly.

MattC
  • 3,794
  • 1
  • 31
  • 47