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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:07:10+00:00 2026-05-15T21:07:10+00:00

I have a problem in a VB.Net class library which I have simplified greatly

  • 0

I have a problem in a VB.Net class library which I have simplified greatly down to the following…

Public MustInherit Class TargetBase

End Class

Public Class TargetOne
    Inherits TargetBase
End Class

Public Class TargetTwo
    Inherits TargetBase
End Class

Public Class TargetManager
    Public Sub UpdateTargets(ByVal Targets As List(Of TargetBase))
        For Each objTarget As TargetBase In Targets
            UpdateTarget(objTarget)
        Next
    End Sub

    Private Sub UpdateTarget(ByVal Value As TargetOne)

    End Sub

    Private Sub UpdateTarget(ByVal Value As TargetTwo)

    End Sub
End Class

This will not compile due to a syntax error on the UpdateTarget(objTarget) line – Overload resolution failed because no accessible ‘UpdateTarget’ can be called without a narrowing conversion

So I changed the For-Each loop to use Object instead of TargetBase…

For Each objTarget As Object In Targets
    UpdateTarget(objTarget)
Next

This now compiles but I get a run-time error – Public member ‘UpdateTarget’ on type ‘TargetManager’ not found.

So I took the obvious next step of making the 2 UpdateTarget() overloads Public (instead of Private).

Public Sub UpdateTarget(ByVal Value As TargetOne)

End Sub

Public Sub UpdateTarget(ByVal Value As TargetTwo)

End Sub

This now works!

I can just about understand why changing it to Object would work but why must these methods be made Public when I am only calling them from within the same class – I would rather they weren’t available outside this class.

Can anybody explain this?

Thanks in advance (and sorry for the length of this question!)

Additional
Thanks for everyones answers so far. Ive got the workaround (make UpdateTarget methods Public) that makes it work. Another workaround would be to do a TypeOf check on objTarget and then DirectCast before calling UpdateTarget, like…

For Each objTarget As Object In Targets
    If TypeOf objTarget Is TargetOne Then
        UpdateTarget(DirectCast(objTarget, TargetOne))
    ElseIf TypeOf objTarget Is TargetTwo Then
        UpdateTarget(DirectCast(objTarget, TargetTwo))
    End If
Next

This will also work – I posted the question because I really wanted to understand why changing visibility of UpdateTarget from Private to Public got rid of the run-time error, totally against my understanding!

  • 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-15T21:07:10+00:00Added an answer on May 15, 2026 at 9:07 pm

    Looks to me like it can’t decide what method to use because you are using the base type of both method parameters. TargetOne/Two are both valid TargetBases so both methods look the same to the resolution engine – meaning it can’t pick.

    However, I’ve no idea why the other changes make it work… let me think, update pending.

    In C# I don’t get this issue because you cannot forward cast TargetBase to TargetOne or TargetTwo… it gives a different compiler error – argument for method is invalid because it cannot implicitly convert base to derived. The first compiler error you mentioned is basically the VB.NET equivalent.

    I spotted this link, but I’m not sure if its for VB or VB.NET – either way, interesting read:
    http://msdn.microsoft.com/en-us/library/tb18a48w.aspx

    This also might relate to Option Strict, and co-variance in VB.NET 2010. This article has a little overloads section, might prove useful:
    http://msdn.microsoft.com/en-us/magazine/ee336029.aspx

    Update: note that I’ve no clue why its suddenly working, this sounds like one for Jon Skeet or Eric Lippert.

    Update 2: one thing I can suggest is for each situation (private to public / use of object) compile the application and review the IL using Reflector. Basically, look for any differences – it could be that the compiler is adding something for you under the hood – either that, or the runtime is able to determine the correct method based on the current type.

    Update 3: think I got it. This quote from the following link:

    “An object is early bound when it is
    assigned to a variable declared to be
    of a specific object type.”

    http://visualbasic.about.com/od/usingvbnet/a/earlybind.htm

    Says that when you specify TargetBase, it’s early bound and compiler complains. When you specify object, it’s late bound and the runtime complains when its private re this link:

    http://msdn.microsoft.com/en-us/library/h3xt2was(VS.80).aspx

    Therefore specifying public works for you. The runtime is obviously able to late bind to the correct overload – a nice feature of late binding that was hidden by VB.NET for you 🙂

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

Sidebar

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.