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

  • Home
  • SEARCH
  • 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 137721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T07:09:03+00:00 2026-05-11T07:09:03+00:00

I’ve been trying to create a simple base class that encapsulates some of my

  • 0

I’ve been trying to create a simple base class that encapsulates some of my conventions for database access. I generally create a sproc named ‘products_retrieve_product’ to select a product based on productID. I would like the method ‘Retrieve’ in the base class to return the type that the derived class supplies in it’s definition. Is what I’m trying to accomplish possible with Generics?

public class MyBaseClass <T> {     private string _className;      public MyBaseClass ()     {         _className = this.GetType().Name;     }      public virtual T Retrieve(int ID)     {         Database db = DatabaseFactory.CreateDatabase();         DbCommand dbCommand = db.GetStoredProcCommand(String.Format('{0}s_Retrieve_{0}', _className));         db.AddInParameter(dbCommand, String.Format('@{0}ID', _className), DbType.Int32, ID);          using (IDataReader dr = db.ExecuteReader(dbCommand))         {             if (dr.Read())             {                 BOLoader.LoadDataToProps(this, dr);             }         }         return (T)this;     } } 
  • 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-11T07:09:04+00:00Added an answer on May 11, 2026 at 7:09 am

    I think that you want to do something like this:

    class MyBaseClass<T> where T : MyBaseClass<T>, new() {     public T Retrieve()     {         return new T();     } }  class Foo : MyBaseClass<Foo> { }  class Program {     public static void Main()     {         Foo f = new Foo();         Foo f2 = f.Retrieve();         Console.WriteLine(f2.ToString());     } } 

    When you run this program, the type name of Foo is printed on the command line. Obviously this is a contrived example, but maybe you can do something more useful when loading from a database in MyBaseClass.Retrieve().

    The key is to add a constraint on T so that it must be an instance of the class itself. This way you can specify the subclass as the generic type when subclassing MyBaseClass<T>.

    I’m not entirely sure if this is a good idea or not, but it looks like it can be done.

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

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Seemingly simple, but I cannot find anything relevant on the web. What is the
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 am trying to loop through a bunch of documents I have to put
I'm making a simple page using Google Maps API 3. My first. One marker
I have some data like this: 1 2 3 4 5 9 2 6

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.