开发者

.NET Class Interface, Inheritance and Library: error does not implement interface member

I want to do this (I'm on silverlight but nothing specific so want to do this also on winform and wpf)

namespace MyComponents
{
    public class IMyManager : ILibManager
    {
        void SetModel(ILibModel model);
    }
}

but get this error

Error 2 'MyComp开发者_JAVA百科onents.IMymanager' does not implement interface member 'lib.manager.ILibManager.SetModel(lib.model.ILibModel)'. 'MyComponents.IMymanager.SetModel(lib.model.ILibModel)' cannot implement an interface member because it is not public. C:...\MyComponents\MyComponents\IMymanager.cs 17 18 MyComponents

Why ? This is the code in Lib

using lib.model;

using System;
using System.Collections.Generic;
using System.Text;

namespace lib.manager
{
    public interface ILibManager
    {
        public void SetModel(ILibModel model);
    }
}

using lib.model;

using System;
using System.Net;
using System.Windows;


namespace lib.manager
{
    public class Manager: IManager
    {
        // Constructor
        public Manager() { 

        }

        public void SetModel(ILibModel model) {

        }

    }
}

namespace lib.model
{
    public interface ILibModel
    {

    }
}


namespace lib.model
{
    public class Model : ILibModel
    {

    }
}


I believe you had two errors here, didn't you? there should be an error saying that SetModel should have a body because IMyManager isn't an interface or an abstract class!

So, I believe you should have a body for that method, and then it has to be "public" since it's part of an implementation of an interface. And you should also rename IMyManager to be "MyManager", since it's not an interface. you should have your class like this:

public class MyManager : ILibManager
{
    public void SetModel(ILibModel model)
    {
        // implementation of SetModel
    }
}

Hope this helps :)


Try this instead:

namespace MyComponents
{
    public class MyManager : ILibManager
    {
        public void SetModel(ILibModel model)
        {
           // ...
        }
    }
}

A class that conforms to an interface (contract!) must implement it in a public manner.


You might also try explicit implementation, such as:

public class MyManager : ILibManager
{
    void ILibManager:SetModel(ILibModel model)
    {
        // ...
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜