2

I'm new to c# programming and I know it's an amateur question so please don't laugh me!

I was declare these interfaces

class derived : iInterface3
{
    double[] d = new double[5];
    public override int MyProperty
    {
        get
        {
            return 5;
        }
        set
        {
            throw new Exception();
        }
    }
    int iProperty
    {
        get
        {
            return 5;
        }
    }
    double this[int x]
    {
        set
        {
            d[x] = value;
        }
    }
}
class derived2 : derived
{

}
interface iInterface
{
    int iProperty
    {
        get;
    }
    double this[int x]
    {
        set;
    }
}
interface iInterface2 : iInterface
{ }
interface iInterface3 : iInterface2
{ }

even i implement all of members of iInterface to derived class but still i recive this error.

'final_exam_1.derived' does not implement interface member 'final_exam_1.iInterface.this[int]'. 'final_exam_1.derived.this[int]' cannot implement an interface member because it is not public.

and this

'final_exam_1.derived' does not implement interface member 'final_exam_1.iInterface.iProperty'. 'final_exam_1.derived.iProperty' cannot implement an interface member because it is not public.

why?

Thanks for your helps in advance!

kmatyaszek
  • 19,016
  • 9
  • 60
  • 65
Armin Rasoulian
  • 271
  • 1
  • 9

4 Answers4

3

You need to add the public access modifier to all members that derive from the class.

By default they will have lower access.

Also, you need to drop the override, as there is nothing to override when implementing an interface. Overriding is when there is a virtual method that you wish to override.

class derived : iInterface3
{
    double[] d = new double[5];

    public int MyProperty
    {
        get
        {
            return 5;
        }
        set
        {
            throw new Exception();
        }
    }

    public int iProperty
    {
        get
        {
            return 5;
        }
    }

    public double this[int x]
    {
        set
        {
            d[x] = value;
        }
    }
}

There are other issues with your code, but those are the reasons why things are not compiling.

Community
  • 1
  • 1
Oded
  • 489,969
  • 99
  • 883
  • 1,009
0

Make iProperty and the indexer public or use explicit interface implementation. The declaration for explicit implementation would look like this: int iInterface3.iProperty.

Mike Zboray
  • 39,828
  • 3
  • 90
  • 122
0

You can't override property int MyProperty, because nothing to override. No int MyProperty in base class/interface.

Hamlet Hakobyan
  • 32,965
  • 6
  • 52
  • 68
0

Yo do have a lot of silly issues

    public override int MyProperty
    {
        get
        {
            return 5;
        }
        set
        {
            throw new Exception();
        }
    }

since you are implementing from a interface, not a base class / abstract class which already has a virtual member hence the override is pointless.

Second issue.

    int iProperty
    {
        get
        {
            return 5;
        }
    }

inherited property cannot be of type private.

Fixed Code:

class derived : iInterface3
{
    readonly double[] d = new double[5];
    public int MyProperty
    {
        get
        {
            return 5;
        }
        set
        {
            throw new Exception();
        }
    }

    public int iProperty
    {
        get
        {
            return 5;
        }
    }

    public double this[int x]
    {
        set
        {
            d[x] = value;
        }
    }
}
class derived2 : derived
{

}
interface iInterface
{
    int iProperty
    {
        get;
    }
    double this[int x]
    {
        set;
    }
}
interface iInterface2 : iInterface
{ }
interface iInterface3 : iInterface2
{ }
Parimal Raj
  • 20,189
  • 9
  • 73
  • 110