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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T21:48:11+00:00 2026-05-18T21:48:11+00:00

I have a class which i realized will not always correctly instantiate and as

  • 0

I have a class which i realized will not always correctly instantiate and as a quick fix, i figured i’d subclass it and shadow a few methods so that the program can continue to run without exploding. When i run the software, calls to the methods resolve to the base’s implementations and not the subclass. I’m using VB.NET with .NET 2.0. Here is an example of what i’m trying to do:

Public Class SuperClass

  Public Sub New ()
    Dim type As Type = GetType(SubClass)
    If (Me.GetType() is type) Then
      //nothing
    Else
      //build real object
    EndIf
  End Sub

  Private Shared _Instance As SuperClass
  Public Shared ReadOnly Property Instance() As SuperClass
    Get
      If (_Instance Is Nothing) Then
        Try
          _Instance = New SuperClass()
        Catch ex As Exception
          Dim result As DialogResult = MessageBox.Show(text, caption, MessageBoxButtons.RetryCancel, MessageBoxIcon.Information)
          If (result = DialogResult.Retry) Then
            _Instance = New SuperClass()
            //this will probably cause problems of its own, but i'll cross that bridge later...
          Else
            _Instance = New SubClass()
          End If
        End Try

      End If
      Return _Instance
    End Get
  End Property

  Public Overridable Function MyFunction() As Integer
     Dim somethingReasonable As Integer //do something for real
     Return somethingReasonable
  End Function

End Class

Public Class SubClass
  Inherits SuperClass

  Public Sub New()
    //doesn't do what cause the exception in the first place
  End Sub

  Public Shadows Function MyFunction() As Integer
    //Do something safe
    Return -1
  End Function    

End Class

I’m not sure why the base class gets called during runtime. When i inspect the object in the debugger it is clearly the SubClass type but the SuperClass methods get called. Access to the object is through the Instance Property.

I’m sure i’m doing something wrong or making some wrong assumption but I can’t figure out what.

Thanks,
brian

  • 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-18T21:48:11+00:00Added an answer on May 18, 2026 at 9:48 pm

    If a method is shadowed rather than overridden, the shadowing methods will not be called when an instance is of subclass type — the method which will be called is determined based on the compile-time type of the receiver rather than the run-time type. This is the fundamental difference between shadowing and overriding.

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

Sidebar

Related Questions

I have a code-first, POCO project in which I am trying to adjust an
Let's say I have a class that allocates some arbitrary member data. There are
In this I need C++ array class template, which is fixed-size, stack-based and doesn't
My application handles with files of type *.mndl which is not more than a
I must develop a simple web application to produce reports. I have a single
i'm kinda new to programs which feature an mvc architecture and i want to
I'm currently doing a lot of things on exception safety. (Herb Sutter's Exceptional C++,
I'm trying to build a list of functions that can be used for arbitrary
As part of one of my projects, there are BeforeBuild tasks that ultimately generate
Basically I am wondering about having this behavior in an app where the newer

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.