如何解决接口错误

时间:2018-02-06 12:57:59

标签: c#

我有四个错误的界面,我无法理解这些错误这里是我的代码。我提到了我发现错误的区域。 在错误1中我收到此错误"键入' ISAPI'在接口列表中不是接口" 在错误2中我得到了这个错误"接口不能包含字段" 在错误3中我得到了这个错误"接口成员不能有定义" 在错误4中我得到了这个错误"接口成员无法定义"

enter code here

using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

namespace InsertFromAndroidToSql

{
public interface SAPI : ISAPI // have error 1
{
    SqlConnection dbConnection; // have error 2
    void ServiceAPI() // have error 3
    {
        dbConnection = DBConnect.getConnection();
    }

    void CreateNewAccount( string Name, 
                           string userName, 
                           string password, 
                           string PhoneNumber, 
                           string CNIC) // have error 4
    {
        if (dbConnection.State.ToString()=="Closed")
        {
            dbConnection.Open();
        }
        string query = "Insert Into UserDetails VALUE ('" + Name + "','" + 
userName + "', '" + password + "', '" + PhoneNumber + "', '" + CNIC + "');";

        SqlCommand command = new SqlCommand(query, dbConnection);
        command.ExecuteNonQuery();
        dbConnection.Close();
    }
}
}

3 个答案:

答案 0 :(得分:0)

回答你的问题

  

如何解决界面错误

  • 错误1:

    • 您的界面必须来自界面。 ISAPI显然不是界面。
  • 错误2:

    • 接口不能包含字段。使用get和/或set将其声明为属性。
  • 错误3:

    • 您无法在界面中定义方法主体。只声明它们。
  • 错误4:

    • 与3相同。

这将是一个有效的界面:

public interface SAPI
{
    SqlConnection dbConnection { get; }
    void ServiceAPI();
    void CreateNewAccount(string Name, string userName, string password, string PhoneNumber, string CNIC);
}

或者将当前的“界面”声明为抽象类

public abstract class SAPI //...

答案 1 :(得分:0)

首先执行此操作并查看错误,它将减少为1错误

using System.Collections.Generic;
  using System.Data.SqlClient;
  using System.Linq;
  using System.Web;

 namespace InsertFromAndroidToSql
  {
   public interface SAPI : ISAPI // have error 1
 {
     //SqlConnection dbConnection; // have error 2   this is fields in 
     // interface you are declaring comment it or remove it 
     void ServiceAPI();    // have error 3   declare it dont define its 
    // functionality in interface
    //{
    // dbConnection = DBConnect.getConnection();
    //}

void CreateNewAccount( string Name, 
                       string userName, 
                       string password, 
                       string PhoneNumber, 
                       string CNIC); // have error 4  same remove its definition
  //{
  //  if (dbConnection.State.ToString()=="Closed")
  //  {
    //    dbConnection.Open();
  //  }
   // string query = "Insert Into UserDetails VALUE ('" + Name + "','" + 
  //userName + "', '" + password + "', '" + PhoneNumber + "', '" + CNIC + 
  //"');";

   // SqlCommand command = new SqlCommand(query, dbConnection);
   // command.ExecuteNonQuery();
   // dbConnection.Close();
  }
 }

一旦你这样做,你的代码就像

 using System.Collections.Generic;
 using System.Data.SqlClient;
 using System.Linq;
 using System.Web;

 namespace InsertFromAndroidToSql
  {
     public interface SAPI : ISAPI // have error 1
      {  

         void ServiceAPI();       
         void CreateNewAccount( string Name, 
                   string userName, 
                   string password, 
                   string PhoneNumber, 
                   string CNIC);
  }
 }

现在你了解了界面的样子,检查" ISAPI" 是否是接口,如果它不是接口,你可以继承或扩展到当前接口。将其删除或将其修复为界面。

答案 2 :(得分:-1)

问题是您正在尝试在接口上定义变量和方法,这是不允许的。下面我复制了Microsoft docs中类和接口的解释。

Class

  

类是一种构造,它允许您通过将其他类型,方法和事件的变量分组在一起来创建自己的自定义类型。一个类就像一个蓝图。它定义了类型的数据和行为。 [..]客户端代码可以通过创建分配给变量的对象或实例来使用它。

Interface

  

接口包含类[..]可以实现的定义或一组相关功能。

那是什么意思?在您的示例中,您有一个对接口或类的引用,我现在假设它是一个接口。让我们说它的定义如下:

public interface ISAPI
{
    void CreateNewAccount( /* parameters omitted*/ );
}

这段代码意味着,当且仅当它们实现所有已定义的函数(在这种情况下,仅ISAPI)时,我们才会认为某些事物是CreateNewAccount()

将某些内容定义为ISAPI,您需要创建一个实现它的类,例如:

public class Sapi : ISAPI {

     public void CreateNewAccount( /**/ ) {
          // Do something that creates an account.
     }
}

Sapi类实际上包含ISAPI接口的实现。

我希望这一点有点清楚,我强烈建议你找一个教程,全面解释如何使用类,抽象类和接口以及它们之间的差异。

相关问题