We help IT Professionals succeed at work.

C# asp.net: interface for a partial class...

Medium Priority
730 Views
Last Modified: 2013-12-17
I'm trying to set up and interface that defines certain fields for all tables.  I'm using a partial class to inherit the definitions from the interface but I keep getting an error  " CPMS_DAL.tbl_Account does not implement interface member IENTBaseEntity.CreateDate"..it does this for each definition in IENTBaseEntity.

Please Help!

Thanks.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
 
namespace CPMS_DAL.Framework
{
    public interface IENTBaseEntity
    {
        DateTime CreateDate { get; set; }
        int CreateByID  { get; set; }
        DateTime UpdateDate { get; set;}
        int UpdateByID { get; set; }
        Binary Version { get; set; }
    }
}
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CPMS_DAL.Framework;
 
namespace CPMS_DAL
{
    public partial class tbl_Accounts : IENTBaseEntity {}
}

Open in new window

Comment
Watch Question

Top Expert 2013

Commented:
Your class tbl_Accounts must implement the interface properties.
So your class tbl_Accounts should have these properties like below:
 public DateTime CreateDate
    {
        get
        {
            throw new NotImplementedException();
        }
        set
        {
            throw new NotImplementedException();
        }
    }

  Same goes for other properties...
You are free to implement the code in get /set  as per your need

Author

Commented:
I'm working from a book and they said that this should be generated everytime the partial class is generated won't this keep that from happening?
Top Expert 2013

Commented:
Yes but then you will need another  partial class in the same assembly that implements the Interface
i.e.
public partial class tbl_Accounts : IENTBaseEntity {}

another class
public partial class tbl_Accounts
{  //that implements the IENTBaseEntity properties
}

Also the interface and be implemented over more than one partial class.
At compile time they are merged to form a whole class.

check this: http://msdn.microsoft.com/en-us/library/wa80x488(VS.80).aspx

Author

Commented:
okay, here is the whole code solution I'm using and still get the same error:


IENTBaseEntity.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
IENTBaseEntity.cs:

namespace CPMS_DAL.Framework
{
public interface IENTBaseEntity
{
DateTime CreateDate { get; set; }
int CreateByID { get; set; }
DateTime UpdateDate { get; set;}
int UpdateByID { get; set; }
Binary Version { get; set; }
}
}

CustomizedEntities.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CPMS_DAL.Framework;

namespace CPMS_DAL
{
public partial class tbl_Accounts : IENTBaseEntity {}
}

ENTBaseData.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CPMS_DAL.Framework
{
public abstract class ENTBaseData where T : IENTBaseEntity
{
public abstract List Select();

public abstract T Select(int id);

public abstract void Delete(CPMSDataContext db, int id);

public void Delete(string connectionString, int id)
{
using (CPMSDataContext db = new CPMSDataContext(connectionString))
{
Delete(db, id);
}
}
}
}

tbl_AccountsData.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CPMS_DAL.Framework
{
public class tbl_AccountsData : ENTBaseData
{
public override List Select()
{
using (CPMSDataContext db = new CPMSDataContext())
{
return db.sp_Accounts_SelectAll().ToList();
}
}

public override tbl_AccountsData Select(int id)
{
using (CPMSDataContext db = new CPMSDataContext())
{
return db.sp_Accounts_SelectSingle(id).SingleOrDefault();
}
}
}
}

Top Expert 2013

Commented:
I am not sure I am understanding you but let me try one more time.
Partial class allows you to split code across multiple classes....
e.g.
public partial class tbl_Accounts : IENTBaseEntity {
//All you properite implemented here
}

is equivalent to combination of all 3 classes below:

public partial class tbl_Accounts
{  //implement CreateDate { get; set; } and CreateByID { get; set; } properties
}
public partial class tbl_Accounts
{  //implement UpdateDate { get; set;} and UpdateByID { get; set; } properties
}
public partial class tbl_Accounts
{  //implement Version { get; set; } property
}

But in your code I see just:
public partial class tbl_Accounts : IENTBaseEntity {}
and no other partial class ...so ideally nobody is implementing the IENTBaseEntity.
Note: Try removing partial keyword ... and still you should get that error...why becuase you class is not implementing the interface

Thats what I understand and not sure if this is something missing in my knowledge about partial classes.

Simple example: http://weblogs.asp.net/gunnarpeipman/archive/2008/06/08/c-and-partial-classes.aspx

Author

Commented:
I understand that partial class lets you split code. This is what I understand: "You can create another file and then create a partial class that is named the same as the entity class and have that inherit the correct interface.  That way, you can regenerate the entity class as offter as you want and not have to worry about cusomizing its code."  

That is why the solution has this piece :

CustomizedEntities.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CPMS_DAL.Framework;

namespace CPMS_DAL
{
public partial class tbl_Accounts : IENTBaseEntity {}
}

Author

Commented:
Hi, I'm pretty new at this.  Sorry to be so definite but I'm trying to understand why I get this error, there is a big disconnect between the the example in the book and what you guys are saying.  I'm hoping to get some help to sort it all out.  I thank you for your time in this.

Thanks.
Top Expert 2013

Commented:
as I said it has nothing to do with partial class....if your class wants to use interface....it has to implement all properties / methods of that interface.
Now if you have partial class this implementation can be split across those partial classes.
In the example of your book...check if there is any other code that says 'partial class tbl_Accounts  "

One more example:
http://www.dotnetspider.com/forum/193095-partial-classes-using-c-asp-net.aspx

Which book are you referring to? Are you using the exact same code or you are trying to learn concepts from the book and implement in your own project?

Note: On this thread...the very first post i.e. your question.. there is "Request Attention" button in Red color. You can use that to request attention from other experts to get some more input.

Author

Commented:
the book I'm using is the WROX programmer to programmer:ASP.NEt 3.5 Enterprise Application Development with Visual Studio 2008 I'm looking at chapter 2 solution.  I don't see any other partial class just methods to add for deleting and updating records.
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
I'm so sorry I didn't get back to anyone on this list I was caught up in a crisis here at work that set an impossible deadline.  please forgive me I will be more attentive next time.  I will try "learned one"'s suggestion and see what happens.  Thank you all for your help I really appreciate it.  Thanks.

Author

Commented:
Hey I'm finally back on this project. i was looking at the solution and I don't see why that would work.  Why would I use an interface when I have to define it every where else.  The whole point of an interface is plug-and-play.

I think the error is coming from  the below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CPMS_DAL.Framework;

namespace CPMS_DAL
{
    public partial class tbl_Accounts : IENTBaseEntity { }
}

the error I get says:
'CPMS_DAL.tbl_Accounts' does not implement interface member 'CPMS_DAL.Framework.IENTBaseEntity.CreateDate'

I get this error for each defined in the interace.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.