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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:07:57+00:00 2026-05-13T21:07:57+00:00

I have several classes, that all derives from SuperClass. When the classes are created,

  • 0

I have several classes, that all derives from SuperClass.

When the classes are created, they all are put into a List(Of SuperClass).
When I go through the list, i would like to downcast the SuperClass object to its baseObject, and put it into the correct list. (I have one list created for each of the sub types of SuperClass).

It is possible to determin:

 If TypeOf SuperClass Is SubClass Then
      listOfSubClass.Add(DirectCast(SuperCLass, SubClass)
 End If

but this is a lot of work, when there are several classes.

By using

SuperClass.GetType.FullName

I get the type of the subClass.

My question is: Is it possible to use this to dynamically cast the SuperClass object?
in psudoCode:

For each SuperClass
     Dim temp As SuperClass.GetType.FullName = _
                       DirectCast(SuperCLass, SuperClass.GetType.FullName 
     list.add(temp)
Next

Edit:
I was hoping I could use a variable instead of the creating one case for each SubClass and do it all in one loop.

  • 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-13T21:07:57+00:00Added an answer on May 13, 2026 at 9:07 pm

    Have you considered using LINQ’s OfType extension method? It wraps an enumerable and filters the elements so that only the ones of the specified type are returned. You could use it like this:

    list.AddRange(superList.OfType(Of SubClass)())
    

    or even:

    list = superList.OfType(Of SubClass)().ToList()
    

    Sorry if my syntax is off, it’s been a while since I’ve used VB.NET


    Edit: Example, as promised:

    namespace Demo.ListFilter
    {
        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Text;
        using System.Collections;
    
        class Program
        {
            private class SuperClass
            {
            }
    
            private class SubClassA :
                SuperClass
            {
            }
    
            private class SubClassB :
                SuperClass
            {
            }
    
            static void Main(string[] args)
            {
                var superList = new List<SuperClass>()
                {
                    new SuperClass(),
                    new SuperClass(),
                    new SuperClass(),
                    new SuperClass(),
                    new SubClassA(),
                    new SubClassA(),
                    new SubClassA(),
                    new SubClassB(),
                    new SubClassB(),
                    new SubClassB(),
                    new SubClassB()
                };
    
                var listA = new List<SubClassA>();
                var listB = new List<SubClassB>();
    
                SplitList(superList, listA, listB);
    
                Console.WriteLine("List A: {0}", listA.Count);
                Console.WriteLine("List B: {0}", listB.Count);
                Console.WriteLine("Press any key to exit...");
                Console.ReadKey();
            }
    
            static void SplitList(IList superList, params IList[] subLists)
            {
                foreach(var subList in subLists)
                {
                    var type = subList.GetType().GetGenericArguments()[0];
                    FilterList(superList, subList, type);
                }
            }
    
            static void FilterList(IList superList, IList subList, Type type)
            {
                var ofTypeMethod = typeof(Enumerable).GetMethod("OfType");
                var genericMethod = ofTypeMethod.MakeGenericMethod(type);
                var enumerable = (IEnumerable)genericMethod.Invoke(null, new[] { superList });
    
                foreach(var item in enumerable)
                {
                    subList.Add(item);
                }
            }
        }
    }
    

    Another Edit: You could also combine the methods like this:

        static void SplitList(IList superList, params IList[] subLists)
        {
            var ofTypeMethod = typeof(Enumerable).GetMethod("OfType");
    
            foreach(var subList in subLists)
            {
                var subListType = subList.GetType();
                var type = subListType.GetGenericArguments()[0];
                var genericOfTypeMethod = ofTypeMethod.MakeGenericMethod(type);
                var enumerable = genericOfTypeMethod.Invoke(null, new[] { superList });
    
                var addRangeMethod = subListType.GetMethod("AddRange");
                addRangeMethod.Invoke(subList, new[] { enumerable });
            }
        }
    

    Don’t forget to add error handling!

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

Sidebar

Ask A Question

Stats

  • Questions 361k
  • Answers 361k
  • 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 This is probably a link order problem. When the GNU… May 14, 2026 at 2:54 pm
  • Editorial Team
    Editorial Team added an answer According to responses in this discussion thread, USB ports can… May 14, 2026 at 2:54 pm
  • Editorial Team
    Editorial Team added an answer Here's my general advice for structuring build streams: +HEAD -… May 14, 2026 at 2:54 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

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.