2012-09-28 98 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”开始。

相关问题