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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:13:36+00:00 2026-05-27T05:13:36+00:00

I have Parent class X, and Parent class H. H has in its field

  • 0

I have Parent class X, and Parent class H.

H has in its field data a reference to a type X.
H’s constructor requires an instance of type X, which is then stored in the reference.

I then derive from X a Child class x1, and from H a child class h1.

h1 will accept in its constructor an instance of X or of x1.

But the methods and properties of x1 which are not already defined in its parent class X will not be available to h1.

How can I permanently cast x1 to the type of x1 within my class h1?

  • 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-27T05:13:36+00:00Added an answer on May 27, 2026 at 5:13 am

    There is no such thing as a “permanent” cast (or, rather, the idea of a “permanent cast” is nonsensical, as that implies that casting does something to the object, which it does not, so there is nothing to either persist or undo). Your variable is of type H, and the type of the variable will never change. Your instance is of type H1, and the instance’s type will never change. Casting simply tells the compiler “Yes, even though I am only referencing this instance as type H, I actually know that it is of type H1, so it’s safe to store it in any variable that can reference an instance of H1.”

    If your structure looks like this:

    class H
    {
    
    }
    
    class X
    {
        public H HValue { get; private set; }
    
        public X(H h)
        {
            HValue = h;
        }
    }
    
    class H1 : H
    {
        public void Foo() { }
    }
    
    class X1 : X
    {
        public X1(H1 h1) : base(h1)
        {
    
        }
    }
    

    Then you’re going to have to store the value for the h1 variable somewhere else if you always want to use that value without downcasting.

    Now, a somewhat “smelly” way of accomplishing what you want (not having to write the casting code every time), you could do this:

    class X1 : X
    {
        public X1(H1 h) : base(h) { }
    
        public new H1 HValue 
        { 
            get { return (H1)base.HValue; }
        }
    }
    

    This would allow you to refer to the same property named HValue anywhere you had a reference to X1 in a variable of type X1. So…

    X x = new X1(new H1());  // x.HValue would be of type H, even though the 
                             // reference itself is H1
    X1 x = new X1(new H1()); // x.HValue would be of type H1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a parent class which contains a child object. I am using set
I have a MustInherit Parent class with two Child classes which Inherit from the
Let's say I have something like this: abstract class Parent { protected function foobar($data)
I have the class B and its parent class A, both in namespace Domain.
In a hierarchical data model i have Parent and Child. Parent has Fields[] and
I have a parent class and child class (inherited from parent). In the child
In Django, when you have a parent class and multiple child classes that inherit
I have a Parent/Child object/mapping as follows: class Parent { int Id; string name;
New to FluentNHibernate =D I have a parent/children classes as follows: public class Parent
I have a parent and child class that both need to implement IDisposable .

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.