C# 如何解决接口错误
我有四个接口错误,我不能理解这些错误这里是我的代码。我提到了我发现错误的地方。 在错误1中,我发现接口列表中的错误类型“ISAPI”不是接口 在错误2中,我得到了这个错误接口不能包含字段 在错误3中,我得到了这个错误接口成员不能有定义 在错误4我得到这个错误接口成员不能有定义C# 如何解决接口错误,c#,C#,我有四个接口错误,我不能理解这些错误这里是我的代码。我提到了我发现错误的地方。 在错误1中,我发现接口列表中的错误类型“ISAPI”不是接口 在错误2中,我得到了这个错误接口不能包含字段 在错误3中,我得到了这个错误接口成员不能有定义 在错误4我得到这个错误接口成员不能有定义 enter code here using System.Collections.Generic; using System.Data.SqlClient; using System.Linq; using System
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();
}
}
}
回答你的问题 如何解决接口错误 错误1: 您的接口必须从接口派生。ISAPI显然不是一个接口。 错误2: 接口不能包含字段。使用get和/或set将其声明为属性。 错误3: 不能在接口中定义方法体。只需申报。 错误4: 与3相同。 这将是一个有效的接口: 或者将当前“接口”声明为抽象类
首先执行此操作并查看错误,它将减少为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是否为接口,如果它不是接口,您可以继承或扩展它到当前接口。将其删除或修复为接口。问题在于您试图在接口上定义变量和方法,这是不允许的。下面我复制了Microsoft文档中关于类和接口的解释 类是一种构造,它使您能够通过将其他类型、方法和事件的变量组合在一起来创建自己的自定义类型。课堂就像一幅蓝图。它定义类型的数据和行为。[…]客户端代码可以通过创建分配给变量的对象或实例来使用它 接口包含类[…]可以实现的定义或一组相关功能 那么这意味着什么呢?在您的示例中,您有一个对接口或类的引用,我现在假设它是一个接口。假设其定义如下:
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接口的实现
我希望这能让事情变得更清楚一点,我强烈建议你找一本教程,全面解释如何使用类、抽象类和接口以及它们之间的区别。错误似乎很清楚,ISAPI不是一个接口,所以不要将其声明为一个接口,其余的可能会正确。请仔细阅读接口是什么,因此,这不是第一个解决问题的地方。您可能希望澄清您在这些错误消息中遇到的问题。我很高兴地告诉你,你在这些问题上有困难,但你需要解释为什么-为什么你认为这四件事中的每一件都应该起作用,或者根据你收到的信息,为什么你在修复这些问题上有问题。我对C很陌生,这是我在C的第一周,所以我无法理解这些错误。我正在解决前两个错误,但无法理解后两个错误。你能引导我走过正确的道路吗code@FarrukhMughalDeclare只表示方法签名,正如您在我的接口示例中看到的那样。您不能在接口中定义这些方法,尽管它意味着逻辑所在的方法体。
public interface ISAPI
{
void CreateNewAccount( /* parameters omitted*/ );
}
public class Sapi : ISAPI {
public void CreateNewAccount( /**/ ) {
// Do something that creates an account.
}
}