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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T22:17:41+00:00 2026-05-12T22:17:41+00:00

Chosen Solution Thanks for the help everyone. I’ve decided to do the following. public

  • 0

Chosen Solution

Thanks for the help everyone. I’ve decided to do the following.

public static class PersonCollection
{
    public static List<string> GetNames(RecordCollection<Person> list)
    {
        List<string> nameList = new List<string>(list.Count);

        foreach (Person p in list)
        {
            nameList.Add(p.Name);
        }

        return nameList;
    }
}


I’m trying to cast a generic collection RecordCollection to a derived collection PersonCollection, but I get a cast exception:

RecordCollection<Person> col = this.GetRecords<Person>(this.cbPeople);
PersonCollection people = (PersonCollection)col;

The reason I’m trying to do this is two-fold:

  1. The derived classes (eg, PersonCollection) can have instance methods (eg, GetLastNames) which shouldn’t be in the base class.
  2. The method GetRecords is generic so I can get a collection of any Record objects.

What is the best approach to solve this in C# 2.0? What is the most elegant approach to solving this?

This is signature of GetRecords:

public RecordCollection<T> GetRecords<T>(ComboBox cb) where T : Record, new()

This is my base implementation:

public abstract class Record : IComparable
{
    public abstract int CompareTo(object other);
}

public class RecordCollection<T> : ICollection<T> where T : Record, new()
{
    private readonly List<T> list;

    public RecordCollection()
    {
        this.list = new List<T>();
    }

    // Remaining ICollection interface here
}

I have derived objects based on that base implementation as follows:

public class Person : Record
{
    public Person()
    {
        // This is intentionally empty
    }

    public string Name
    {
        get;
        set;
    }

    public override int CompareTo(object other)
    {
        Person real = other as Person;
        return this.Name.CompareTo(real.Name);
    }
}

public class PersonCollection : RecordCollection<Person>
{

}
  • 1 1 Answer
  • 1 View
  • 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-12T22:17:41+00:00Added an answer on May 12, 2026 at 10:17 pm

    Your approach does not work because cast does not convert instances of one class to instances of another class.

    You did not give the code of GetRecords<> method, but presumably GetRecords returns RecordCollection, and not PersonCollection (it has new RecordCollection somewhere in the code, doesn’t it?).

    You cannot cast RecordCollection to PersonCollection unless this particular instance actually is PersonCollection. Presicely because it does not have these additional methods.

    This is like

    SomeClass obj=new SomeClass();
    DerivedClass o=(DerivedClass)obj; //throws exception
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public class Anagram { public static void main(String[] args) { String a = Despera
I have the 2 following entities: @Entity @Table(name = brand) public class Brand {
I have a JTable with custom TableCellRenderer. public class DateCellRenderer extends DefaultTableCellRenderer { private
I just can't figure out or find any solution here, so I chose to
I'm using the chosen UI library to select multiple values for a post param
I am using chosen.js ( http://harvesthq.github.com/chosen/ ) and I have a select box that
I'm using Chosen JQuery Plugin for a ASP.NET MVC (Razor) Project. For a Dropdownlist
I’ve chosen to take this as a question in its own right since it
I have chosen diploma work in university. It's a mini social network. But now
I'm not sure why the Chosen Plugin is not being called when my form

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.