Click here to Skip to main content
15,894,291 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I would like to implement the Interface concept in my application , So ijust tried some thing like below but its going something wrong

Interface

C#
interface Grid
{
    DataTable Gridbind(string FromDate, string ToDate);

}


Implementation Class

C#
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

C#
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()
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?
Posted
Updated 22-Mar-15 21:43pm
v2
Comments
Sinisa Hajnal 23-Mar-15 3:25am    
Move your instantiation into the postback. There is nothing wrong with the code, except obviously your "obj" is not created.

Set breakpoint on new GridImplenetationClass and into Page_Load and see what happens in what order :)
King Fisher 23-Mar-15 3:31am    
I moved now it works.Can't we instantiate the obj Globally?

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:
C#
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(/* ... */);
Are you getting the point?

—SA
 
Share this answer
 
v2
Comments
King Fisher 23-Mar-15 3:50am    
I'm getting this error "Error 1 'IGrid.Gridbind': explicit interface declaration can only be declared in a class or struct
"
Sergey Alexandrovich Kryukov 23-Mar-15 3:57am    
And I suggested to do it for the class, precisely. Probably, you screwed up something somewhere. Can you show some code sample manifesting that problem. It would be better if you could reduce it to the simplest and self-contained sample, not depending on your other declarations.
—SA
King Fisher 23-Mar-15 3:55am    
Sorry I mention declared with in Interface, sorry. :)
Sergey Alexandrovich Kryukov 23-Mar-15 3:58am    
No problem.
Will you accept my answer formally now?
In all cases, your follow-up questions will be welcome.
—SA
King Fisher 23-Mar-15 4:02am    
Still I'm getting null while Instantiate globally .Can i show my Code?
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.

C#
public class GridImplementationClass : Admin_PerformanceIncentive
{
    public static DataTable Gridbind(string FromDate, string ToDate)
    {
        // your existing code
    }
}


C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        DataTable dt = GridImplementationClass.Gridbind(FromDate.ToShortDateString(), Todate.ToShortDateString());
    }
}



No need to instantiate the GridImplementationClass class to use the Gridbind method.
 
Share this answer
 
Comments
King Fisher 23-Mar-15 5:14am    
Actually I have attended 3-4 interviews, they asked me that have you used Interface? so i decided to implement interface in my Project ,that' it :)
Sinisa Hajnal 23-Mar-15 5:19am    
This is terrible reason. If you don't need it, don't use it.
King Fisher 23-Mar-15 5:23am    
How do i know whether its needed or not ? I think its applicable in this case. if not ,then which case can i use Interface? can u please explain where i use Interfaces? In many interviews its asked by Interviewer.
F-ES Sitecore 23-Mar-15 5:38am    
They ask you that question because they are wanting to know if you have understanding of interfaces etc and if you have used them before. Trust me when I say that using interfaces because you think you should is as bad as not understanding them at all. Rather than trying to "trick" your way to a job by misrepresenting your understanding, read some articles on interfaces (there are many on the net if you google) and get to a stage where you genuinely understand them and their uses and when you use them and when you don't.

They're commonly used for dependency inject\inversion of control, unit testing, and general OOP practices where you're only interested in the methods an object exposes rather than the object's concrete class.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900