2011-03-18 150 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

我認爲這個錯誤很明顯:你沒有實現所有的接口成員,這當然是必需的。