user6015054
user6015054

Reputation:

Partial class extending multiple method class

Hi so i have one interface with 2 methods which is implemented by an abstract class and one partial class in 2 separate file which extends the abstract class

Ex:

public inteface Iterface
{
    void A();
    void B();
}

public abstract class Abstract : Interface
{
    public abstract A();
    public abstract B();
}


// Partial1.cs    
public partial class Partial : Abstract
{
    public override A(){}
}

// Partial2.cs
public partial class Partial : Abstract
{
    public override B(){}
}

I get the error that Partial does not implement Abstract correctly, how come? If i would add to Partial1.cs the B() so that i would have

// Partial1.cs
public partial class Partial : Abstract
{
     public override A(){}
     public override B(){}
}

all good, but why i am not allowed to implement the Abstract method in whatever file containing the Partial i want?

I've tried also with the following

// Partial1.cs    
public partial class Partial : Abstract
{
     public override A(){}
}

// Partial2.cs
public partial class Partial
{
    public override B(){}
}

Initially i wanted just to use the Interface but i had the same error "Partial class does not implement Interface correctly" so i though that maybe i cannot do so.

Is there any way of doing what i'm trying here? Implement the methods in whichever Partial class file i want ?

Thank you

Upvotes: 1

Views: 3936

Answers (2)

user6015054
user6015054

Reputation:

Found reason why it did not worked.

A partial class defined in assembly a.b.c is not the same as defined in assembly a.b

So i did not care to look about assembly info, in order for my partial class to have worked as i wanted i had to add them in the same assembly.

D'oh!

EX:

namespace A.BL.Repository.ARepo 

should be used wherever the partial class is present

Upvotes: 0

Svek
Svek

Reputation: 12858

Short Answer:

It depends on your compiler/IDE.
For example, it is possible in some versions of Visual Studio you may not be able to break up an interface across multiple partial classes.


You can do it.

In LINQPad and VS2017 this will compile just fine...

public interface I1
{
    void A();
    void B();
}

partial class Foo : I1
{
    public void A() {}
}

partial class Foo : I1
{
    public void B() {}
}

void Main()
{
    Foo foo = new Foo();
    foo.A();
    foo.B();
}

Consider using an alternative design

The alternative and viable use-case for partial classes would be to break down your class against different multiple interfaces. For example:

Interfaces

public inteface I1
{
    void A();
}

public inteface I2
{
    void B();
}

Breaking up a class 1-1 with Interfaces

public partial class Partial : I1
{
    public void A() { }
}

public partial class Partial : I2
{
    public void B() { } 
}

Which ultimately would compile to

public partial class Partial : I1, I2
{
    public void A() { }
    public void B() { } 
}

Upvotes: 3

Related Questions