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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:35:03+00:00 2026-05-27T11:35:03+00:00

I have a parent class that is also a factory. For example: Public Class

  • 0

I have a parent class that is also a factory. For example:

Public Class Factory
    Public Function clone() as Factory
        ' Some logic here
        ' return something
    End Function

    Public Function all() as List (Of Factory)
        ' Some logic here
        ' return something
    End Function
End Class

And then an inherited one

Public Class BookFactory
    inherits Factory
End Class

I can use inflection in the Factory class to generate the proper extended objects when called by the inherited one. myBookFactory.clone() will then return a BookFactory instance and not only a Factory instance.

The problem: this BookFactory instance will be cast as Factory, since the type of the function is Factory and not BookFactory.

I’d like to do something like

Public Class Factory
    Public Function clone() as Me.GetType()
        ' Some logic here
        ' return something
    End Function

    Public Function all() as List (Of  Me.GetType())
        ' Some logic here
        ' return something
    End Function
End Class

So the returned value would be correctly cast and avoid having to do this each time:

Dim myBookFactory2 = DirectCast(myBookFactory1.clone(), myBookFactory1.getType())

How can I do this?

  • 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-27T11:35:04+00:00Added an answer on May 27, 2026 at 11:35 am

    This seems to be a variation on asking for covariant return types. As you have noticed, this is not supported by VB.NET (or C# for that matter). Typically this is asked in the context of overriding virtual methods, where it is still not allowed. There are several alternatives, each with their own pros and cons.

    Use a generic template argument to specify the derived class

    This is similar to the way IComparable<T> is most commonly implemented.

    Public Class Factory(Of T As Factory)
        Public Function Clone() As T
            'use GetType(T) to determine derived type
        End Function
    End Class
    
    Public Class BookFactory
        Inherits Factory(Of BookFactory)
    End Class
    

    Additionally, if you can add a New constraint to the Factory (eg: Factory(Of T {New, Factory(Of T)})) base class, you may be able to avoid using reflection.

    However, this does not prevent the accidental (or potentially malicious) mistake of declaring a class like this:

    Public Class EvilFactory
        Inherits Factory(Of BookFactory)
        'hmmm, now clone will be making the wrong type
    End Class
    

    Also, this approach makes it impossible to create a list of factories of different types without resorting to another base class below Factory(Of T) or declaring the list as being of object.

    Make new methods on the derived classes that return the specific type you want.

    Public Class Factory
        Public Function Clone() As Factory
            'create derived class, but return as base
        End Function
    End Class
    
    Public Class BookFactory
        Inherits Factory
    
        Public Function CloneBooks() As BookFactory
            Return CType(Me.Clone(), BookFactory)
        End Function
    End Class
    

    This allows you to hide the cast for those times when you know you have a BookFactory and want to get another BookFactory. It also lets you treat all factory types polymorphically in the normal inheritance sense. However, if you have an object typed as Factory, you will still get back an object type as Factory.

    Reconsider the inheritance relationship

    Depending on how these classes are used, it may not make sense to use the inheritance relationship here. If you are only concerned with not retyping code, you may want to look into code generation instead.

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

Sidebar

Related Questions

I have a parent and child class that both need to implement IDisposable .
I have a private method def __pickSide(self): in a parent class that I would
In Django, when you have a parent class and multiple child classes that inherit
New to FluentNHibernate =D I have a parent/children classes as follows: public class Parent
I have the following basic classes (cut down for this question): public class Parent
Suppose I have a parent class that implements a protocol: @interface GameViewController : UIViewController<GamePrizeDelegate>
I'm using some namespaced controllers that also inherit from a parent controller. In each
I have a parent class which contains a child object. I am using set
I have a parent class and child class (inherited from parent). In the child
I have a MustInherit Parent class with two Child classes which Inherit from the

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.