Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: Does not implement interface member

Tags:

c#

interface

I'm new to c#, and I'm kinda struggling with implementing interfaces, gonna be very grateful if someone helps me with this, with an explanation. Thanks

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace interfejsi
 interface Figura
{
  String Plostina ();
  String Perimeter ();
}
}
 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 namespace interfejsi
{
 class Kvadar : Figura
{
    int a,b,c;
    public  String Perimetar(int a, int b, int c)
    {
        return (a + b + c).ToString();
    }
    public String Plostina(int a, int b, int c)
    {
        return (a * b * c).ToString();
    }
}

}

like image 981
kinsell Avatar asked Apr 19 '14 13:04

kinsell


People also ask

Does not implement interface member Cannot implement an interface member because it is not public?

The error thrown is usually “Cannot implement an interface member because it is not public”. If an interface member method is implemented in your class without an access modifier, it is by default private. To change the visibility, you can either change the interface from public to internal.

What does an interface Do C#?

An interface defines a contract. Any class or struct that implements that contract must provide an implementation of the members defined in the interface. An interface may define a default implementation for members. It may also define static members in order to provide a single implementation for common functionality.

Can interface be internal?

The internal interface is properties and methods that can be accessed only from other methods of the object, they are also called "private" (there are other terms, we will meet them further). The external interface is the properties and methods available outside the object, they are called "public".


2 Answers

You need to implement the exact functions that sit in the interface(with the same number of input params)..

So in you case change your interface to:

interface Figura
{
   String Perimetar(int a, int b, int c)
   String Plostina(int a, int b, int c)
}

Or change you implementation to functions with no params.

like image 69
Amir Popovich Avatar answered Oct 31 '22 22:10

Amir Popovich


The Method definitions do not match. In the interface the Method String Plostina () is defined, but the class does not have that method. The method in the class has a different signature, in the class it looks like String Plostina(int a, int b, int c).

To implement an interface, the Name, return type and parameters (amount, order and type) must match.

like image 30
SynerCoder Avatar answered Nov 01 '22 00:11

SynerCoder