Solved

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

Posted on 2009-07-06
16
557 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

0
Comment
Question by:Samantha1667
  • 7
  • 4
16 Comments
 
LVL 41

Expert Comment

by:guru_sami
Comment Utility
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
0
 

Author Comment

by:Samantha1667
Comment Utility
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?
0
 
LVL 41

Expert Comment

by:guru_sami
Comment Utility
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
0
 

Author Comment

by:Samantha1667
Comment Utility
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();
}
}
}
}

0
 
LVL 41

Expert Comment

by:guru_sami
Comment Utility
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
0
 

Author Comment

by:Samantha1667
Comment Utility
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 {}
}
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:Samantha1667
Comment Utility
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.
0
 
LVL 41

Expert Comment

by:guru_sami
Comment Utility
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.
0
 

Author Comment

by:Samantha1667
Comment Utility
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.
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 50 total points
Comment Utility
If you are going to implement an interface, then you need to define events, methods, and properties that match the signatures in the interface definition:

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

Each class would need read/write properties that match the name and return types (implicit), or the return types (explicit):

Implicit example:

    public class MyClass : IENTBaseEntity
    {
        public DateTime CreateDate { get; set; }
        public int CreateByID { get; set; }
        public DateTime UpdateDate { get; set; }
        public int UpdateByID { get; set; }
        public Binary Version { get; set; }
    }

Explicit example:

    public class AnotherClass : IENTBaseEntity
    {
        DateTime IENTBaseEntity.CreateDate { get; set; }
        int IENTBaseEntity.CreateByID { get; set; }
        DateTime IENTBaseEntity.UpdateDate { get; set; }
        int IENTBaseEntity.UpdateByID { get; set; }
        Binary IENTBaseEntity.Version { get; set; }
    }
0
 

Author Comment

by:Samantha1667
Comment Utility
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.
0
 

Author Comment

by:Samantha1667
Comment Utility
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.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
comparing string in C# 3 32
.net Multiple Row Form Fields Data Entry 4 16
Problem to page 4 16
Different Delete Messages 7 10
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now