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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:20:51+00:00 2026-05-29T10:20:51+00:00

This is a Two (2) Part Question about Generics I’ve got to create several

  • 0

This is a Two (2) Part Question about Generics

I’ve got to create several similar classes to model similarly designed database tables.

All tables contain an ID int and a Text nvarchar(50) field. One or two may contain a few other fields.

I rarely use generics, but I see examples of it on here quite frequently. This is my largest attempt to create a generic class that is used in another generic class.

My basic construct is as follows, and I will point out with a comment what does not work and the error message Visual Studio 2010 is displaying:

public class IdText {

  public IdText(int id, string text) {
    ID = id;
    Text = text;
  }

  public int ID { get; private set; }

  public string Text { get; private set; }

}

public class TCollection<T> : IEnumerable<T> where T : IdText {

  private List<T> list;

  public TCollection() {
    list = new List<T>();
  }

  public void Add(int id, string text) {
    foreach (var item in list) {
      if (item.ID == id) {
        return;
      }
    }
    list.Add(new T(id, text)); // STOP HERE
    // Cannot create an instance of the variable type 'T'
    // because it does not have the new() constraint
  }

  public T this[int index] {
    get {
      if ((-1 < 0) && (index < list.Count)) {
        return list[index];
      }
      return null;
    }
  }

  public T Pull(int id) {
    foreach (var item in list) {
      if (item.ID == id) {
        return item;
      }
    }
    return null;
  }

  public T Pull(string status) {
    foreach (var item in list) {
      if (item.Text == status) {
        return item;
      }
    }
    return null;
  }

  #region IEnumerable<T> Members

  public IEnumerator<T> GetEnumerator() {
    foreach (var item in list) yield return item;
  }

  #endregion

  #region IEnumerable Members

  System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
    return list.GetEnumerator();
  }

  #endregion

}

Visual Studio’s IntelliSence wants me to add list.Add(T item), but I need to create this first.

I have attempted to re-write the offending line list.Add(new T(id, text)); as list.Add(new IdText(id, text));, but then I am reprimanded with the message “cannot convert from IdText to T“.

How exactly do I get around this?

Next: When I go in to actually create a version of this IdText class later, I am not sure how exactly I can use this new class in the TCollection class I have designed for it.

For example, given this derived class:

public class ManufacturedPart : IdText {

  public ManufacturedPart(int id, string partNum, string description)
    : base(int id, string partNum) {
    Description = description;
  }

  public string Description { get; private set; }

}

…would I need to also derive a special version of TCollection to accompany it, like so?

public class ManufacturedParts<T> : IEnumerable<T> where T : ManufacturedPart {

  // OK, now I'm lost! Surely this can't be right!

}
  • 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-29T10:20:51+00:00Added an answer on May 29, 2026 at 10:20 am

    1) You could use the new() constraint, make your properties public and add a parameterless constructor:

    public class IdText
    {
        public IdText()
        {
        }
    
        public IdText(int id, string text)
        {
            ID = id;
            Text = text;
        }
    
        public int ID { get; set; }
    
        public string Text { get; set; }
    
    }
    
    public class TCollection<T> : IEnumerable<T> where T : IdText, new()
    {
    
        private List<T> list;
    
        public TCollection()
        {
            list = new List<T>();
        }
    
        public void Add(int id, string text)
        {
            foreach (var item in list)
            {
                if (item.ID == id)
                {
                    return;
                }
            }
            list.Add(new T { ID = id, Text = text }); 
        }
    }
    

    2) You have multiple options:

    If you want your collection to store any IdText (ManufacturedPart or anything else that derived from IdText):

    TCollection<IdText> ss = new TCollection<IdText>();
    

    The above, for now, can only store IdText as you instantiate objects in the Add(int, string) method, but if you provide a Add(T object) method, it could store any IdText instance.

    If you want your collection to only contains ManufacturedParts:

    public class ManufacturedParts<T> : TCollection<T> where T : ManufacturedPart, new()
    {
         // Provide here some specific implementation related to ManufacturedParts
         // if you want. For example, a TotalPrice property if ManufacturedPart
         // has a Price property.
    }
    
    TCollection<ManufacturedPart> ss2 = new ManufacturedParts<ManufacturedPart>();
    

    or even simpler, if your collection doesn’t provide any additional method depending on the type of the stored objects:

    TCollection<ManufacturedPart> ss2 = new TCollection<ManufacturedPart>();
    

    Even simpler, if your goal is to only store objects, a custom collection isn’t needed:

    List<IdText> ss2 = new List<IdText>();  // Uses the built-in generic List<T> type
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a two part question. The first is a specific question about DataSets
This is a two-part question: 1. The original .NET print classes (in System.Drawing.Printing) are
This is a two-part question about adding a third-party library (JAR) to an Android
This is a two part question in hopes that I can understand more about
This is a two part question. A dumb technical query and a broader query
This is a two part question. Is it possible to take advantage of xVal
This is a two-part question: First, I am interested to know what the best
This is a two-part question. I'm using jQuery for a project and wanting to
This is actually a two part question. First,does the HttpContext.Current correspond to the current
This is kinda....a two part question. The first one is much more important than

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.