我如何在此处访问接口方法。 [英] How Do I Access Interface Methods Here.

查看:113
本文介绍了我如何在此处访问接口方法。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的应用程序中实现接口概念,所以我尝试了下面的一些东西,但它出错了



接口



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?

推荐答案

最好不要违反命名约定:你的界面应该命名为的iGrid 。它不会解决你的问题,但会让生活更轻松。 :-)



另外,不要在班级名称中使用class一词。是不是很明显?



我同意Sinisa Hajnal:你没有显示所有代码有助于找出为什么 obj 为null,但您可以在调试器下轻松找到它。你的问题不同:理解。如果你想使用接口,实际使用它们。



考虑一下:
Better don't violate naming conventions: your interface should be named IGrid. It won't fix your problems but will make life easier. :-)

Also, don't use the word "class" in the name of the class. Isn't it obvious?

I agree with Sinisa Hajnal: you don't show the all the code helping to figure out why obj is null, but you can easily find it out under the debugger. Your problem is different: understanding. If you want to use interfaces, actually use them.

Consider this:
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(/* ... */);

你明白了吗?



-SA


你还没有说明为什么要使用接口,并且已经指出你的代码甚至没有使用它。如果你没有真正需要一个接口,并且你正在寻找全局代码,那么我的建议是根本不使用该接口而是使用静态方法。



You haven't said why you want to use an interface, and as already pointed out your code wasn't even using it. If you have no real need for an interface and you are looking for "global" code then my advice is to not use the interface at all and use a static method instead.

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.


这篇关于我如何在此处访问接口方法。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆