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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T16:51:34+00:00 2026-06-14T16:51:34+00:00

So for example I have two classes: Class A { string property1; string property2;

  • 0

So for example I have two classes:

Class A
{
  string property1;
  string property2;
}

Class B : A
{
  string property3;
  string property4;
  ....
} 

So B inherits class A’s properties. They are sitting in a list, that is sitting in a dictionary

Dictionary <string, List<A>> myDictionary = new Dictionary<string, List<A>>();

List<A> myList = new List<A>();

There is one Dictionary, containing many List’s, that all contain a mix of Class A & B objects.
While looping through, I am trying to access some properties from Class B objects, I have an if statement to find them but the program still thinks they are of type Class A and throws an error when I try and use a property3 or property4. For example:

string key = string key in dictionary;
string index = object position in list;

myDictionary[key][index].property3.someMethod();

Is there a way to tell the program that this is a class B object and allow the properties 3 & 4 to be used?

  • 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-14T16:51:35+00:00Added an answer on June 14, 2026 at 4:51 pm

    Cast the object safely as a B-type object, then check for null

    var obj = myDictionary[key][index];
    
    var bObj = obj as B;
    if (bObj != null)
    {
         bObj.someMethod();
    }
    

    Although, I would also probably say it seems like your design is off. Ordinarily, I wouldn’t expect something like this. Normally, if you’re using inheritance, you’d want a design that allows them to be used interchangeably. For example, you might implement the behavior on A as a no-op, but override it on B to actually do something. This would make it so that consuming classes need not care whether the “A” thing is really an A or a B instance.

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

Sidebar

Related Questions

I have two classes, one which inherits the other, for example:- public class A
I have two classes in a PHP application, B extending A for example. Class
I have two domain classes, Alpha and Beta. class Beta{ String betaName int age
So say, for example I have two classes: Base = declarative_base() class Vendor(Base): __tablename__
I have two classes A and B. Class B inherits class A as shown
Consider the following example. I have an interface MyInterface, and then two abstract classes
I have two classes, one nested in the other. Public Class Operation Public Property
I have two classes from example using Fluent NHibernate mapping. Fluent NHibernate mapping is
I have two classes: one base class an one derived class The base class
Lets say I have two classes: public class A { public virtual int Id

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.