Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 896707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:46:16+00:00 2026-05-15T14:46:16+00:00

So here is the code: interface A<T> { T Save(); } interface B :

  • 0

So here is the code:

interface A<T>
{
    T Save();
}

interface B : A<B>
{
}

class BusinessBase<T>
{
    T Save();
}

class D<T, U> : BusinessBase<T>
    where T : BusinessBase<T>
    where U : B<U>
{
    new U Save()
    {
        base.Save(); // this is where the error is because T can't be converted to U
    }
}

class Concrete : D<Concrete , B>, B
{
}

So with all that what I am hoping to have is a base class that defines all the methods of A which are really just redirects to methods in C. But to support IoC and CSLA it has to be this way. So what I am looking for is to have the save in D return U not T so as to match the interface signature..

I have been staring at this for a while and can’t seem to figure out what I am missing.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T14:46:17+00:00Added an answer on May 15, 2026 at 2:46 pm

    Ok so I just want to apologize for some of the bad code that was in my example. But hopefully if you looked at it for a while you could see where it went wrong. But here is the answer that I have come up with. I think there might be a better way in the conversion but I wanted a solution that didn’t require me changing the BusinessBase class as that is a core CSLA component and not one I want to change. Here is the code I have come up with that is working:

    interface A<T>
    {
        T Save();
    }
    
    interface IConcreteInterface : A<IConcreteInterface>
    {
    }
    
    class BusinessBase<T>
        where T : BusinessBase<T>
    {
        public T Save()
        {
            return (T)this;
        }
    }
    
    class D<T, U> : BusinessBase<T>
        where T : BusinessBase<T>, A<U>
        where U : A<U>
    {
        public new U Save()
        {
            return (U)(object)base.Save();
        }
    }
    
    class ConcreteClass : D<ConcreteClass, IConcreteInterface>, IConcreteInterface
    {
    }
    

    The change that made it work was this:
    return (U)(object)base.Save();

    Before I was didn’t put in the (U) cast in the sample because it woudn’t compile like that. As there is no relationship between T and U that can be determined. So the only way around this is to cast the return T from Save to (object) which of course will then be capable of casting to anything.

    You notice I also added a type constraint for a little added protection against cast errors in that I am making sure that T is of type A and that U is of type A. That ensures that both types have the same interface.

    If someone has something prettier I am open to suggestions. But for now this is working and I feel somewhat good about it.
    You can do what I did which was at least require T to implement It isn’t pretty and you can enforce a little more

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok the error is showing up somewhere in this here code if($error==false) { $query
Here is the code I'm having problems with: The interface: public interface anInterface {
Here a code to demonstrate an annoying problem: class A { public: A(): m_b(1),
Here is code example class A{ int i; public: A(int i) : i(i) {}
Okay, I need some help here. This is the same old "can't use an
Here goes: I have a view that looks like this: public interface IAddressView {
I had this code from a previous question, but its not compiling: public interface
Here is the code: @Repository public interface AccountRepository extends JpaRepository<Account, Long> {} JpaRepository from
The script is on jsfiddle here : CODE What it does at the moment
Here is code my jqgrid editing through form. $(#DataEnergy).jqGrid('navGrid', '#pagergrid', {}, //options {editdata: {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.