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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:47:55+00:00 2026-06-17T12:47:55+00:00

I have a few Abstract classes that have the core functionality of some UserControls

  • 0

I have a few Abstract classes that have the core functionality of some UserControls in my project. I also have two separate implementations both deriving from the Abstract classes; lets call them AbstractImpl1 and AbstractImpl2. In addition, I have Collections of the Abstract UserControls that I iterate through in several places.

The issue I’m having is InvalidCastExceptions – I need to iterate through the implementations rather than the abstact UserControls to get to certain properties (visual). Is there a clean way of dealing with this other than doing a try/catch?

Example:

In my project I have Abstract classes: AbsUserControl
Then I have two separate implementations of them: AbstractImpl1 and AbstractImpl2

In my main form I have ObservableCollection<AbsUserControl> absControlCollection and then iterating through items I do foreach(AbstractImpl1 userControl in absControlCollection) to be able to access the visual aspects. The issue is that ObservableCollection<AbsUserControl> absControlCollection could either be comprised of AbstractImpl1 or AbstractImpl2 objects.

Is there a clean way of dealing with this? Right now, every place I iterate through the objects in absControlCollection I surround it with a try/catch block, trying to iterate through AbstractImpl1 objects then catching an InvalidCastException and proceeding with iterating through AbstractImpl2 objects.

Let me know if any more information is needed. Thanks!

  • 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-06-17T12:47:56+00:00Added an answer on June 17, 2026 at 12:47 pm

    An alternative to OfType; casting without exceptions:

    foreach(AbsUserControl control in absControlCollection)
    {
        if(control is AbstractImpl1)
        {
            AbstractImpl1 i1 = (AbstractImpl1)control;
            DoStuff(i1);
        }
        if(control is AbstractImpl2)
        {
            AbstractImpl2 i2 = (AbstractImpl2)control;
            DoOtherStuff(i2);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a few classes such that: public class XMLStatusMessage extends XMLMessage {} public
I have a collection of classes that inherit from an abstract class I created.
I have few own APIs with around 2000 classes overall. Some of them use
I have used a lot of abstract classes both for polymorphism, and obviously inheriting
I have few pages in following structure. --Project |---WebContect |----Admin/ * |----Author/ * |----Readonly/
In C#, I have a class hierarchy with a couple of abstract base classes
So I did some refactoring and two of my classes now look exactly the
Abstract classes and interfaces play very a important role in Java and they have
I have an java interface which has few abstract methods. public interface Interface {
I have a few objects that are incredibly similar. In fact, they use STI

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.