2012-09-28 76 views
0

我不斷收到此錯誤,我不確定我做錯了什麼。錯誤1 'Home.Services.InventoryImpl' 不實現接口成員 'Home.Services.InventorySvc.CreateInventory(Home.Services.InventoryImpl)'不實現接口成員 - C#

我的接口代碼

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using Home; 
using Home.Domain; 

namespace Home.Services 
{ 
    public interface InventorySvc 
    { 
     void CreateInventory(InventoryImpl CreateTheInventory); 
    } 
} 

我的實現代碼

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using Home.Domain; 
using System.IO; 
using System.Runtime.Serialization; 
using System.Runtime.Serialization.Formatters.Binary; 

namespace Home.Services 
{ 
    public class InventoryImpl: InventorySvc 
    { 
     public void CreateTheInventory(CreateInventory createinventory) 
     { 

      FileStream fileStream = new FileStream 
      ("CreateInventory.bin", FileMode.Create, 
      FileAccess.Write); 
      IFormatter formatter = new BinaryFormatter(); 
      formatter.Serialize(fileStream, createinventory); 
      fileStream.Close(); 
     } 
    } 
} 

回答

9

您的方法名爲CreateTheInventory,但在界面中稱爲CreateInventory。方法簽名必須與接口成員完全匹配,以便編譯器將該方法視爲實現接口成員,並且名稱不匹配。

此外,參數類型不匹配 - 在您的實現中,您有CreateInventory作爲參數類型,但接口採用類型爲InventoryImpl的參數。

如果你糾正了這兩個錯誤,你的代碼應該會生成。

+1

同意在這裏添加我的兩分錢後,鍵入您的:InventorySvc,右鍵單擊界面並選擇「實現接口」,這將創建您的方法(和屬性)作爲底座,然後你只需填寫實際的代碼。 – iMortalitySX

2

InventorySvc接口定義:

void CreateInventory(InventoryImpl CreateTheInventory); 

但你已經實現了:

public void CreateTheInventory(CreateInventory createinventory) 

看到區別?

0

該類中的方法簽名與接口方法的簽名不匹配。

使用鼠標懸停在接口名稱上時出現的智能標記來創建接口實現。這使一切都適合你。

此外,你應該打電話給你的界面IInventorySvc。接口名稱的指導原則規定,在邏輯名之前應該放置一個大寫的「I」,即使後者以「I」開始。

相關問題