2011-03-18 153 views
3

我想实现IUpdatable。实施接口错误:类不实现接口成员

错误1“WebRole1.InfoManager”不实现接口成员“System.Data.Services.IUpdatable.ClearChanges()” 我得到的是说我没有实现所有的接口成员的错误,但我实现了一些当然不是全部。我没有把孔码放在希望你能理解的地方。

using System; 
    using System.Collections.Generic; 
    using System.Linq; 
    using System.Web; 
    using System.Data.Services; 
    using Microsoft.WindowsAzure; 
    using Microsoft.WindowsAzure.ServiceRuntime; 
    using Microsoft.WindowsAzure.StorageClient; 

    namespace WebRole1 
    { 
    public class InfoManager : IUpdatable 
    { 
     private TableServiceContext context; 

    // To Generate DataConnectionString and svcClient 
    private TableServiceContext GetContext() 
    { 
    //Implemented code 
    } 

    public CommentManager() 
    { 
     context = GetContext(); 
    } 


    // To get my table infos 
    public IQueryable<Info> Infos 
    { 
     get 
     { 
      return context.CreateQuery<Info>("Infos").AsTableServiceQuery(); 
     } 
    } 
    // Creating the resource and cheking the compatibility of the type and do an add Object 

    public Object CreateResource(string containerName, string fullTypeName) 
    { 
     //Implemented Code 
    } 

    // Return the instance of the resource represented by the object 
    public Object ResolveResource(Object resource) 
    { 
     return resource; 
    } 

    public void SaveChanges() 
    { 
     context.SaveChangesWithRetries(); 
    } 

    public void setValue(Object targetResource, string propertyName, Object propertyValue) 
    { 
    //Implemented Code 
    } 

} 

}

回答

8

它是一个接口,因此您必须实现所有成员 - 无论您是否愿意。

这个错误不会消失,直到您完整地实现接口。你可以在你正在实现的方法范围内做你想做的事情,比如说甚至提出了一个NotImplementedException--但是你的实现,所以编译器很高兴。

我不会容忍无知(你还是应该学习怎么样了个为什么),但我会提供可以在你的学习帮助和提示,如果不出意外,未来生产力:

从在Visual Studio中,当你有类代码文件打开这是实现该接口,那么你可以有VS吐出代码,你...

class MyClass : IMyInterface // <- hover mouse and click the drop down that appears 

从下拉列表中,你应该看到选项Implement Interface 'IMyInterface',点击它并瞧!它会自动为你生成骨架方法体。

+0

好吧,如果我只需要一些方法,我可以提出NotImplementedException,这是更快,谢谢。 – 2011-03-18 14:49:16

+0

虽然这当然是正确的方法,但这并不是一个好习惯。作为一个同事,我总是会质疑为什么你没有使用所有的方法来实现一个接口。你基本上是在说谎,除非接口规定这种实现是允许的。 – jdmichal 2011-03-18 14:53:19

+0

如果您想快速查看自动生成的更新。如果你想手动完成,那么定义方法主体并省略任何异常提升会比_adding_'throw'更快。 – 2011-03-18 14:55:42

1

我不明白的问题是什么。如果您实现了一个接口,则必须在该接口中实现所有方法。否则,编译器会报错。

+0

它给了我一个错误,即使我执行 – 2011-03-18 14:43:14

+0

@ 404Dreamer_ML是的。它会给出错误,除非你实现_all_方法。这就是实现接口的意思,即实现其所有方法。 – jdmichal 2011-03-18 14:54:19

1

我认为这个错误很明显:你没有实现所有的接口成员,这当然是必需的。