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 1036727
In Process

The Archive Base Latest Questions

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

I’m struggling with some Generic constraint issues when trying to implement a library that

  • 0

I’m struggling with some Generic constraint issues when trying to implement a library that allows inheritance and hoping someone can help.

I’m trying to build up a class library that has 3 flavours to it, each building on top of the other. To me it seemed like a perfect opportunity to use Generics as I can’t quite do what I want through pure inheritance. The code’s below (this should paste straight into VS) with some explanation afterwards:

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

namespace Test
{
    #region Base Classes

    public class GenericElement { }

    /// <summary>Visit to a GenericElement</summary>
    public class Generic_Visit<E> where E : GenericElement
    {
        public E Element { get; set; }
    }

    /// <summary>Collection of Visits</summary>
    public class Generic_Route<V, E>
        where V : Generic_Visit<E>
        where E : GenericElement
    {
        public List<V> Visits { get; set; }
        public Double Distance { get; set; }
    }

    /// <summary>Collection of Routes</summary>
    public class Generic_Solution<R, V, E>
        where R : Generic_Route<V, E>
        where V : Generic_Visit<E>
        where E : GenericElement
    {
        public List<R> Routes { get; set; }

        public Double Distance
        {
            get
            {
                return this.Routes.Select(r => r.Distance).Sum();
            }
        }
    }

    #endregion

    #region TSP Classes

    public class Concrete_TSPNode : GenericElement { }

    public abstract class Generic_TSPVisit<E> : Generic_Visit<E>
        where E : Concrete_TSPNode
    {
        public Double Time { get; set; }
    }

    public abstract class Generic_TSPRoute<V, E> : Generic_Route<V, E>
        where V : Concrete_TSPVisit
        where E : Concrete_TSPNode
    {
        public Double Time
        {
            get
            {
                return this.Visits.Select(v => v.Time).Sum();
            }
        }
    }

    public abstract class Generic_TSPSolution<R, V, E> : Generic_Solution<R, V, E>
        where R : Concrete_TSPRoute
        where V : Concrete_TSPVisit
        where E : Concrete_TSPNode
    {
        public Double Time
        {
            get
            {
                return this.Routes.Select(r => r.Time).Sum();
            }
        }
    }

    public class Concrete_TSPVisit : Generic_TSPVisit<Concrete_TSPNode> { }

    public class Concrete_TSPRoute : Generic_TSPRoute<Concrete_TSPVisit, Concrete_TSPNode> { }

    public class Concrete_TSPSolution : Generic_TSPSolution<Concrete_TSPRoute, Concrete_TSPVisit, Concrete_TSPNode> { }

    #endregion

    #region VRP

    public class Concrete_VRPNode : Concrete_TSPNode { }

    public abstract class Generic_VRPVisit<E> : Generic_TSPVisit<E> where E : Concrete_VRPNode
    {
        public Double Capacity { get; set; }
    }

    public abstract class Generic_VRPRoute<V, E> : Generic_TSPRoute<V, E>
        where V : Concrete_VRPVisit
        where E : Concrete_VRPNode
    {
        public Double Capacity
        {
            get
            {
                return this.Visits.Select(v => v.Capacity).Sum();
            }
        }
    }

    public abstract class G_VRPSolution<R, V, E> : Generic_TSPSolution<R, V, E>
        where R : Concrete_VRPRoute
        where V : Concrete_VRPVisit
        where E : Concrete_VRPNode
    {
        public Double Capacity
        {
            get
            {
                return this.Routes.Select(r => r.Capacity).Sum();
            }
        }
    }

    public class Concrete_VRPVisit : Generic_VRPVisit<Concrete_VRPNode> { }

    public class Concrete_VRPRoute : Generic_VRPRoute<Concrete_VRPVisit, Concrete_VRPNode> { }

    public class Concrete_VRPSolution : Generic_TSPSolution<Concrete_VRPRoute, Concrete_VRPVisit, Concrete_VRPNode> { }

    #endregion
}

The idea behind the code is that there are a set of base classes that expose properties using Generics. This allows me to have strongly typed collections for example.

There are then 2 of the 3 stages build upon these, TSP and VRP in the example which have 4 concrete classes (these are what the developer using the class library should be interacting with as the generic constraints just get a bit crazy) – Element, Visit, Route and Solution.

There are also some classes prefixed Generic for both TSP and VRP. These allow the inheritance that I want as it exposes the Generic Types. If I don’t use these (say Concrete_VRPRoute inherits Concrete_TSPRoute) then I have to keep casting the type of item returned by the Visits collection to get the Capacity property for example.

I’m fairly confident all the types line up correctly, but when I try to build I get the following errors and I really don’t know how to tackle them.

Error 1 The type ‘V’ cannot be used as
type parameter ‘V’ in the generic type
or method ‘Test.Generic_Route’.
There is no implicit reference
conversion from ‘V’ to
‘Test.Generic_Visit’.

Error 2 The
type ‘V’ cannot be used as type
parameter ‘V’ in the generic type or
method ‘Test.Generic_Solution’.
There is no implicit reference
conversion from ‘V’ to
‘Test.Generic_Visit’.

Error 3 The
type ‘R’ cannot be used as type
parameter ‘R’ in the generic type or
method ‘Test.Generic_Solution’.
There is no implicit reference
conversion from ‘R’ to
‘Test.Generic_Route’

Error 4 The type ‘V’ cannot be used as
type parameter ‘V’ in the generic type
or method
‘Test.Generic_TSPRoute’. There is
no implicit reference conversion from
‘V’ to ‘Test.Concrete_TSPVisit’.

Error 5 The type ‘V’ cannot be used as
type parameter ‘V’ in the generic type
or method
‘Test.Generic_TSPSolution’.
There is no implicit reference
conversion from ‘V’ to
‘Test.Concrete_TSPVisit’.

Error 6 The
type ‘R’ cannot be used as type
parameter ‘R’ in the generic type or
method
‘Test.Generic_TSPSolution’.
There is no implicit reference
conversion from ‘R’ to
‘Test.Concrete_TSPRoute’.

Error 7 The
type ‘Test.Concrete_VRPVisit’ cannot
be used as type parameter ‘V’ in the
generic type or method
‘Test.Generic_TSPSolution’.
There is no implicit reference
conversion from
‘Test.Concrete_VRPVisit’ to
‘Test.Concrete_TSPVisit’.

Error 8 The
type ‘Test.Concrete_VRPRoute’ cannot
be used as type parameter ‘R’ in the
generic type or method
‘Test.Generic_TSPSolution’.
There is no implicit reference
conversion from
‘Test.Concrete_VRPRoute’ to
‘Test.Concrete_TSPRoute’.’Test.Concrete_TSPRoute’.

  • 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-16T14:45:06+00:00Added an answer on May 16, 2026 at 2:45 pm

    It’s a piece of generic cake. You need to define the generic classes in terms of themselves. A recursive generic definition.

    Base Classes:

    public class Generic_Element<E>
        where E : Generic_Element<E>
    {
    }
    
    /// <summary>Visit to a Generic_Element</summary>
    public class Generic_Visit<V, E>
        where V : Generic_Visit<V, E>
        where E : Generic_Element<E>
    {
        public E Element { get; set; }
    }
    
    /// <summary>Collection of Visits</summary>
    public class Generic_Route<R, V, E>
        where R : Generic_Route<R, V, E>
        where V : Generic_Visit<V, E>
        where E : Generic_Element<E>
    {
        public List<V> Visits { get; set; }
        public Double Distance { get; set; }
    }
    
    /// <summary>Collection of Routes</summary>
    public class Generic_Solution<S, R, V, E>
        where S : Generic_Solution<S, R, V, E>
        where R : Generic_Route<R, V, E>
        where V : Generic_Visit<V, E>
        where E : Generic_Element<E>
    {
        public List<R> Routes { get; set; }
    
        public Double Distance
        {
            get
            {
                return this.Routes.Select(r => r.Distance).Sum();
            }
        }
    }
    

    TSP Classes:

    public class Generic_Tsp_Element<E> : Generic_Element<E>
    where E : Generic_Tsp_Element<E>
    {
    }
    
    /// <summary>Visit to a Generic_Element</summary>
    public class Generic_Tsp_Visit<V, E> : Generic_Visit<V, E>
        where V : Generic_Tsp_Visit<V, E>
        where E : Generic_Tsp_Element<E>
    {
        public Double Time { get; set; }
    }
    
    /// <summary>Collection of Visits</summary>
    public class Generic_Tsp_Route<R, V, E> : Generic_Route<R, V, E>
        where R : Generic_Tsp_Route<R, V, E>
        where V : Generic_Tsp_Visit<V, E>
        where E : Generic_Tsp_Element<E>
    {
        public Double Time
        {
            get
            {
                return this.Visits.Select(v => v.Time).Sum();
            }
        }
    }
    
    /// <summary>Collection of Routes</summary>
    public class Generic_Tsp_Solution<S, R, V, E> : Generic_Solution<S, R, V, E>
        where S : Generic_Tsp_Solution<S, R, V, E>
        where R : Generic_Tsp_Route<R, V, E>
        where V : Generic_Tsp_Visit<V, E>
        where E : Generic_Tsp_Element<E>
    {
        public Double Time
        {
            get
            {
                return this.Routes.Select(r => r.Time).Sum();
            }
        }
    }
    
    public class Concrete_Tsp_Element : Generic_Tsp_Element<Concrete_Tsp_Element> { }
    
    public class Concrete_Tsp_Visit : Generic_Tsp_Visit<Concrete_Tsp_Visit, Concrete_Tsp_Element> { }
    
    public class Concrete_Tsp_Route : Generic_Tsp_Route<Concrete_Tsp_Route, Concrete_Tsp_Visit, Concrete_Tsp_Element> { }
    
    public class Concrete_Tsp_Solution : Generic_Tsp_Solution<Concrete_Tsp_Solution, Concrete_Tsp_Route, Concrete_Tsp_Visit, Concrete_Tsp_Element> { }
    

    VRP Classes:

    public class Generic_Vrp_Element<E> : Generic_Element<E>
    where E : Generic_Vrp_Element<E>
    {
    }
    
    /// <summary>Visit to a Generic_Element</summary>
    public class Generic_Vrp_Visit<V, E> : Generic_Visit<V, E>
        where V : Generic_Vrp_Visit<V, E>
        where E : Generic_Vrp_Element<E>
    {
        public Double Capacity { get; set; }
    }
    
    /// <summary>Collection of Visits</summary>
    public class Generic_Vrp_Route<R, V, E> : Generic_Route<R, V, E>
        where R : Generic_Vrp_Route<R, V, E>
        where V : Generic_Vrp_Visit<V, E>
        where E : Generic_Vrp_Element<E>
    {
        public Double Capacity
        {
            get
            {
                return this.Visits.Select(v => v.Capacity).Sum();
            }
        }
    }
    
    /// <summary>Collection of Routes</summary>
    public class Generic_Vrp_Solution<S, R, V, E> : Generic_Solution<S, R, V, E>
        where S : Generic_Vrp_Solution<S, R, V, E>
        where R : Generic_Vrp_Route<R, V, E>
        where V : Generic_Vrp_Visit<V, E>
        where E : Generic_Vrp_Element<E>
    {
        public Double Capacity
        {
            get
            {
                return this.Routes.Select(r => r.Capacity).Sum();
            }
        }
    }
    
    public class Concrete_Vrp_Element : Generic_Vrp_Element<Concrete_Vrp_Element> { }
    
    public class Concrete_Vrp_Visit : Generic_Vrp_Visit<Concrete_Vrp_Visit, Concrete_Vrp_Element> { }
    
    public class Concrete_Vrp_Route : Generic_Vrp_Route<Concrete_Vrp_Route, Concrete_Vrp_Visit, Concrete_Vrp_Element> { }
    
    public class Concrete_Vrp_Solution : Generic_Vrp_Solution<Concrete_Vrp_Solution, Concrete_Vrp_Route, Concrete_Vrp_Visit, Concrete_Vrp_Element> { }
    

    The final result is non-generic concrete classes that can be used like this:

    var e = new Concrete_Tsp_Element();
    var v = new Concrete_Tsp_Visit();
    v.Element = e;
    v.Time = 0.5;
    var r = new Concrete_Tsp_Route();
    r.Visits = new List<Concrete_Tsp_Visit>(new[] { v });
    r.Distance = 2.1;
    var s = new Concrete_Tsp_Solution();
    s.Routes = new List<Concrete_Tsp_Route>(new[] { r });
    Console.WriteLine(s.Distance);
    Console.WriteLine(s.Time);
    Console.ReadLine();
    

    Enjoy!
    Enjoy!

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

Sidebar

Ask A Question

Stats

  • Questions 517k
  • Answers 517k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Lisp cons cells can't be directly modeled as Tcl values… May 16, 2026 at 7:59 pm
  • Editorial Team
    Editorial Team added an answer You can perform your housekeeping in a separate script which… May 16, 2026 at 7:59 pm
  • Editorial Team
    Editorial Team added an answer First of all, the speed of your computer won't be… May 16, 2026 at 7:59 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a French site that I want to parse, but am running into
Seemingly simple, but I cannot find anything relevant on the web. What is the
this is what i have right now Drawing an RSS feed into the php,
I want to count how many characters a certain string has in PHP, but
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

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.