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,817
  • 11
  • 84
  • 120
sam
  • 149
  • 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,817
  • 11
  • 84
  • 120
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 Wizard Says No More War
  • 64,101
  • 26
  • 136
  • 201
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,924
  • 1
  • 32
  • 48