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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:32:20+00:00 2026-05-25T00:32:20+00:00

I have recently worked with vb.net and got a problem of a factory pattern

  • 0

I have recently worked with vb.net and got a problem of a factory pattern with generic. I have already solved it though, I don’t know why it doesn’t work in vb.net. I have several years experience of c#, and I sometimes write codes like a following in c# and it works without any problems.

public class GenericTest
{
    public static void Test()
    {
        var test = Factory<TestItem>.GetTest(ProcessType.Derived);
        test.Do();
    }
}

public class TestItem
{
    public int Property1 { get; set; }
}

public class TestBase<T> where T: class
{
    public virtual void Do()
    {
        Console.WriteLine(typeof(T).ToString());
    }
}

public class TestDerived : TestBase<TestItem>
{
    public override void Do()
    {
        Console.WriteLine(typeof(TestItem).ToString());
    }
}

public class Factory<T> where T:class
{
    public static TestBase<T> GetTest(ProcessType processType)
    {
        switch (processType)
        {
            case ProcessType.Derived :
                return new TestDerived() as TestBase<T>;
            default:
                return new TestBase<T>();
        }
    }
}

public enum ProcessType
{
    Base,
    Derived
}

but when I ported the above codes to vb.net, the compiler gives me an error.

Public Class GenericTest
    Public Shared Sub Test()
        Dim test = Factory(Of TestItem).GetTest(ProcessType.Derived)
        test.DoIt()
    End Sub
End Class    

Public Class TestItem
    Public Property Property1 As Integer
End Class

Public Class TestBase(Of T As Class)
    Public Overridable Sub DoIt()
        Console.WriteLine(GetType(T).ToString())
    End Sub
End Class

Public Class TestDerived
    Inherits TestBase(Of TestItem)

    Public Overloads Sub DoIt()
        Console.WriteLine(GetType(TestItem).ToString())
    End Sub
End Class

Public Class Factory(Of T As Class)
    Public Shared Function GetTest(processType As ProcessType) As TestBase(Of T)
        Select Case processType
            Case VB_Sandbox.ProcessType.Derived
                ' This line gives me a compile error saying 'cannot convert TestDerived to TestBase(Of T)'
                Return DirectCast(New TestDerived(), TestBase(Of T))
            Case Else
                Return New TestBase(Of T)
        End Select
    End Function
End Class

Public Enum ProcessType
    Base
    Derived
End Enum

So, I introduced a following interface to make everything okay and that actually solved my problem.

Public Class TestBase(Of T As Class)
    Implements ITest
    Public Overridable Sub DoIt() Implements ITest.DoIt
        Console.WriteLine(GetType(T).ToString())
    End Sub
End Class

Public Class TestDerived
    Inherits TestBase(Of TestItem)

    Public Overloads Sub DoIt()
        Console.WriteLine(GetType(TestItem).ToString())
    End Sub
End Class

Public Class Factory(Of T As Class)
    Public Shared Function GetTest(processType As ProcessType) As ITest
        Select Case processType
            Case VB_Sandbox.ProcessType.Derived
                Return DirectCast(New TestDerived(), ITest)
            Case Else
                Return New TestBase(Of T)
        End Select
    End Function
End Class

Public Interface ITest
    Sub DoIt()
End Interface

Is this just a limitation of vb.net?

  • 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-25T00:32:21+00:00Added an answer on May 25, 2026 at 12:32 am

    The main reason is that DirectCast in VB is much more like the C# (cast) operator than the as operator. The C# code works because as will just return null when TestDerived isn’t a TestBase<T> (which is any time T isn’t TestItem). DirectCast will actually fail if TestDerived isn’t a TestBase(Of T), and so you see the error.

    You can get similar functionality in VB by using the Typeof Is construct, or using TryCast().

    Alternatively, you could have TestDerived actually inherit from TestBase<T>, (thus making TestDerived<T>) which would make everything work without casting in both languages.

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

Sidebar

Related Questions

I have mostly worked with ASP.NET WebForms and more recently MVC. I am now
I am a .Net developer but recently shifted to PHP world. I have worked
Recently, I have worked in a project were TDD (Test Driven Development) was used.
I have recently installed .net 3.5 SP1. When I deployed a compiled web site
I have recently upgraded some of my web applications to ASP.NET 3.5 by installing
I have recently written an application(vb.net) that stores and allows searching for old council
I recently upgraded a C# project from .NET 3.5 to .NET 4. I have
I've looked around and tried many suggestions but none have worked. I recently migrated
I have worked with Django before and have recently seen the Play framework .
I have recently just started working with Lucene (specifically, Lucene.Net) and have successfully created

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.