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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T20:26:44+00:00 2026-05-10T20:26:44+00:00

Am I doing something wrong or is it not possible to specify a generic

  • 0

Am I doing something wrong or is it not possible to specify a generic class as a constraint to a generic method?

I have been playing around with generics and db4o (open source object database) and am writing a test program (see code below) to store and retrieve some user defined generic collections.

I am attempting to write a generic method (see GetCollectionFromDb below) to retrieve a specifically typed collection from the database. Unfortunately, the code below returns a compiler generated error for the line:

 MyCollection1 collection3 =                   GetCollectionFromDb<MyCollection1>(Collection1Name); 

The error message is:

The type 'GenericsTest.MyCollection1'cannot be used as type parameter 'T' in the generic type or method 'GenericsTest.Program.GetCollectionFromDb<T>(string)'. There is no implicit reference conversion from'GenericsTest.MyCollection1' to 'GenericsTest.MyCollectionBase<GenericsTest.MyCollection1>'. 

I would appreciate any suggestions as to what I may be doing wrong or how I could approach this differently to reach the deisred outcome.

using System; using System.Collections; using System.Collections.Generic; using System.Collections.ObjectModel; using Db4objects.Db4o;   namespace GenericsTest      {         public class Entity1         {             public string SomeProperty { get; set; }         }          public class Entity2         {             public string SomeProperty { get; set; }         }          public abstract class MyCollectionBase<T> : Collection<T>         {             protected MyCollectionBase() : this('') { }              protected MyCollectionBase(string pCollectionName)              {                 CollectionName = pCollectionName;              }              public string CollectionName { get; set; }         }          public class MyCollection1 : MyCollectionBase<Entity1>         {             public MyCollection1(string pCollectionName) :                                           base(pCollectionName) { }              public void DoSomeWorkOnCollection1() {}         }          public class MyCollection2 : MyCollectionBase<Entity2>         {             public MyCollection2(string pCollectionName) :                                           base(pCollectionName) { }              public void DoSomeWorkOnCollection2() { }         }          public class Program         {             public static IObjectContainer db = null;              public static void Main(string[] args)             {                 const string Collection1Name = 'Entity1Collection';                 const string Collection2Name = 'Entity2Collection';                 db = Db4oFactory.OpenFile('Test.db');                  Entity1 entity1 = new Entity1();                 MyCollection1 collection1 = new MyCollection1(Collection1Name);                 collection1.Add(entity1);                 db.Store(collection1);                  Entity2 entity2 = new Entity2();                 MyCollection2 collection2 = new MyCollection2(Collection2Name);                 collection1.Add(entity1);                 db.Store(collection2);                  db.Commit();                 db.Close();                 db = Db4oFactory.OpenFile('Test.db');                  MyCollection1 collection3 =                         GetCollectionFromDb<MyCollection1>(Collection1Name);             }              private static T GetCollectionFromDb<T>(string pCollectionName)                                                   where T : MyCollectionBase<T>             {                 IList<T> queryResult = db.Query((T c) =>                                           c.CollectionName == pCollectionName);                 if (queryResult.Count != 0) return queryResult[0];                  return null;             }         }      } 
  • 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. 2026-05-10T20:26:45+00:00Added an answer on May 10, 2026 at 8:26 pm

    Just follow the T:

        // ...     {        //...        MyCollection1 collection3 = GetCollectionFromDb<MyCollection1>(Collection1Name);      }      private static T GetCollectionFromDb<T>(string pCollectionName) where T : MyCollectionBase<T>     {         IList<T> queryResult = db.Query((T c) => c.CollectionName == pCollectionName);         if (queryResult.Count != 0) return queryResult[0];         return null;     } 

    would translate into:

        private static MyCollection1 GetCollectionFromDb<MyCollection1>(string pCollectionName) where T : MyCollectionBase< MyCollection1 >     {         IList< MyCollection1 > queryResult = db.Query((MyCollection1 c) => c.CollectionName == pCollectionName);         if (queryResult.Count != 0) return queryResult[0];         return null;     } 

    Which is not what you want since MyCollection1 derives off MyCollectionBase< Entity1 > and not MyCollectionBase< MyCollection1 >, which is why you got the error. If you want the constraint to work, you will probably have to use a second type identifier to express the type being used in the generic collection.

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

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Done by hand: L ::= { L } | {… May 12, 2026 at 12:48 am
  • Editorial Team
    Editorial Team added an answer Hi I am just adding some information. It seems you… May 12, 2026 at 12:48 am
  • Editorial Team
    Editorial Team added an answer In the debugger, one can edit whatever is loaded in… May 12, 2026 at 12:48 am

Related Questions

When running FindBugs on my project, I got a few instances of the error
I'm trying to include a vCard export function in an existing page full of
The executeTime below is 30 seconds the first time, and 25 seconds the next
Is it possible to add a relative directory (ie, foo/bar/plugh) to the java classpath

Trending Tags

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

Top Members

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.