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

Related Questions

I have a block of code that basically intializes several classes, but they are
I have several message queues that have specific messages on them. I've created classes
If I have interface IFoo, and have several classes that implement it, what is
I have several service classes that have static methods and offer a service to
I have a need for methods in several classes that must always follow a
I have been tasked with converting several php classes into java classes, which is
I'm looking for a way to get a list of all classes that derive
I have a native/unmanaged C++ library with a number of classes that I would
I have several classes that I want to select .group1-1 .group1-2 .group1-3, each one
I have several classes in my application, all of which have a Name property

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.