问题描述
我想在我的应用程序中实现接口概念,所以我尝试了下面的一些东西,但它出错了
接口
I would like to implement the Interface concept in my application , So ijust tried some thing like below but its going something wrong
Interface
interface Grid
{
DataTable Gridbind(string FromDate, string ToDate);
}
实施班级
Implementation Class
public class GridImplementationClass : Admin_PerformanceIncentive, Grid
{
public DataTable Gridbind(string FromDate,string ToDate)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constring"].ConnectionString);
using (SqlCommand cmd = new SqlCommand("Incentive_Sp", con))
{
con.Open();
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@FromDate", FromDate.ToString());
cmd.Parameters.AddWithValue("@ToDate", FromDate.ToString());
cmd.Parameters.AddWithValue("@type", 2);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt= new DataTable();
da.Fill(dt);
con.Close();
return dt;
}
}
}
另一个班级
Another Class
public partial class Admin_PerformanceIncentive : System.Web.UI.Page
{
GridImplementationClass obj = new GridImplementationClass();//Something wrong here.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable();
dt= obj.Gridbind(FromDate.ToShortDateString(), Todate.ToShortDateString());
}
}
}
GridImplementationClass obj = new GridImplementationClass()
在 obj
中它显示NULL并且在调试时它不会移动到下一行。
它在同一时间闪烁在我点击F10的时候
如何解决?
GridImplementationClass obj = new GridImplementationClass()
In obj
it shows NULL and it doesn't move to next line while debugging.
Its blink on the same line while i hit F10
How to fix it?
推荐答案
public class GridImplementationClass : AdminPerformanceIncentive, IGrid
{
// explicit interface implementation will help you
// to hide implementation from the user of
// the class reference, you will need to use only
// the interface reference,
// to improve discipline of your code:
DataTable IGrid.Gridbind(string FromDate, string ToDate) { /* ... */ }
// ...
}
// ...
GridImplementationClass @object = new GridImplementationClass();
// it's very likely that you never need to use this object,
// it seems so because of the name of your class "...Implementation";
IGrid grid = @object; // works, according to general rules
// of assignment compatibility
// for derived types
// or, better, directly IGrid grid = new GridImplementationClass();
// ...
DataTable dataTable = grid.Gridbind(/* ... */);
你明白了吗?
public class GridImplementationClass : Admin_PerformanceIncentive
{
public static DataTable Gridbind(string FromDate, string ToDate)
{
// your existing code
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = GridImplementationClass.Gridbind(FromDate.ToShortDateString(), Todate.ToShortDateString());
}
}
无需实例化GridImplementationClass类即可使用Gridbind方法。
No need to instantiate the GridImplementationClass class to use the Gridbind method.
这篇关于我如何在此处访问接口方法。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!