0

Given the following:

public class A
{
  public A()
  {
  
  }

}

public class B : A
{
  public int b;
  
  public B()
  {
    
  }
}

public A a = new B();
print(a.b); // I'd like to have many classes that inherit from A, and storing
// one of them on a single variable. Then access the variables that belong to the //derived class

My goal here is to create several classes that inherit from a base class. Then assign one of the classes to a variable (whose type must be of type "A") and access the variables of the derived class. Is it possible?

9
  • 2
    No, that's now how C# works in general. What problem do you think that this will help you to solve? Commented Sep 29, 2021 at 6:42
  • My goal here is to create a dictionary that contains variables that derive from class A, then when I pick a random dictionary item, I should be able to access the variables of the derived class. Is it possible? Commented Sep 29, 2021 at 6:43
  • What do you mean "variables that derive from class A"? Are you using "variables" when you mean "instances"? Commented Sep 29, 2021 at 6:43
  • 1
    You're still describing a proposed solution to a problem, and it's not going to work. You say you're going to have a dictionary containing various items and you're going to pick one at random. How are you or the compiler going to know what "variables" you can actually use with that instance? Commented Sep 29, 2021 at 6:45
  • 1
    you should rethink your probolem. When you have a dictionary containing items of your base-class, there's no way to access members of a possible derivant. A should generally not know anything about B. In fact that hardly contradicts the LISKOW-principle. Commented Sep 29, 2021 at 6:53

1 Answer 1

1

That is not how inheritance works.

A child can inherit from a parent, but a parent does not inherit from nor have access to anything that is only on the child level.

But you can test to see if it is the subtype, and then explicitly cast, or do the cast while checking.

 A one = new B();
 A two = new A();

 //Check if one is of type B
 if (one.GetType() == typeof(B))
 {
        //Now you can explicitly cast it.
        B newVar = (B)one; 
 }

if (two.GetType() == typeof(B))
{
        //False, will not reach this point
}

//Cast and check at same time to new variable called first
if(one is B first)
{
        //Logic here using first
}

if (two is B second)
{
        /False, will not reach this point
}
Sign up to request clarification or add additional context in comments.

1 Comment

Would casting to a type given to me by a variable be possible? For example: Type someType = typeof(C); var c = (someType)A ; // obviously doesn't work, but is there a way to make something like it work?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.