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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T05:58:03+00:00 2026-06-01T05:58:03+00:00

I have classes structured like this: Public MustInherit Class A ‘ several properties End

  • 0

I have classes structured like this:

Public MustInherit Class A
  ' several properties
End Class

Public Class B
  Inherits A
  ' several properties
End Class

Public MustInherit Class C
  Protected _X As A
  Public ReadOnly Property X As A
    Get
      Return _X
    End Get
  End Property
End Class

Public Class D
  Inherits C
  Private _X As B
  Public ReadOnly Property X As B
    Get
      Return _X
    End Get
  End Property
  Sub New
    _X = New B
  End Sub
End Class

Is there a modifier I can use on property X in class D which will cause X to be returned as B from an instance of D and A from an instance of D that is evaluated as C?

i.e.

Dim d As New D
Response.Write((d.X Is Nothing) & "<br>")
Dim c As C = d
Response.Write(c.X Is Nothing)

In both cases I want X to not be Nothing

I realize I could modify D as follows:

Public Class D
  Inherits C
  Private __X As B
  Public ReadOnly Property X As B
    Get
      Return __X
    End Get
  End Property
  Sub New
    __X = New B
    _X = __X
  End Sub
End Class

Is there a cleaner solution?

  • 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-01T05:58:04+00:00Added an answer on June 1, 2026 at 5:58 am

    You should avoid new code using Shadows, but if you really need this construct, I get the results you’re expecting with just the addition of MyBase._X=_X to D‘s New (Option 1) which could also be done by having a New in C that D calls, then does the opposite assignment _X = DirectCast(MyBase._X, B) (Option 2).

    Of course another option, depending upon your actual intention, is to drop the Private _X As B in D and just cast the _X to B in your shadowed X. (Option 3)

    Here is my code with Options 1 and 2 commented out and currently performing Option 3.

    Public MustInherit Class C
        Protected _X As A
        ''Option 2:
        'Protected Sub New(X As A)
        '    _X = X
        'End Sub
        Public Overridable ReadOnly Property X As A
            Get
                Return _X
            End Get
        End Property
    End Class
    
    Public Class D
        Inherits C
        'Commented out for option 3:
        'Private Shadows _X As B
        Public Shadows ReadOnly Property X As B
            Get
                'Commented out for option 3:
                'Return _X
                Return DirectCast(_X, B)
            End Get
        End Property
        Sub New()
            ''Option 1,3: (Comment out for option 2)
            _X = New B
            ''Option 1:
            'MyBase._X = _X
            ''Option 2:
            'MyBase.New(New B)
            '_X = DirectCast(MyBase._X, B)
        End Sub
    End Class
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have classes that are structured like the following: public class Forecast { [Key]
I have an hierarchical structure of classes like this public class Class1 { internal
I have classes which have automatic properties only like public customerName {get; set;}. They
I have a Java class that looks like this: public class My_ABC { int
I have this structure of classes: public class L3Message { public int Number {
I have got the following class structure, and have plenty of classes like C
If I have classes of Type A and B: public class A { public
If I have a directory structure like this: - index.php - public - img
I have this C# class structure that I would like to refactor to use
I have a set of core, complicated JavaScript data structures/classes that I'd like to

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.