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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:32:47+00:00 2026-05-22T22:32:47+00:00

I’ve inherited some code and it is making me cringe when I look at

  • 0

I’ve inherited some code and it is making me cringe when I look at it. Is there more elegant way to write the following?

Dim myItem As DTO.MyBaseClass = Nothing
Dim myType As String = GetTypeString()
Select Case myType
  Case Is = "Case1"
    myItem = Bus.BusManager(Of DTO.MyClass1).Read()
  Case Is = "Case2"
    myItem = Bus.BusManager(Of DTO.MyClass2).Read()
'... etc etc for 30 lines

Is there a way to make a map from the string to the class type and then just have a line like so? Or something similar?

myItem = Bus.BusManager(Of MappingDealy(myType)).Read()
  • 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-22T22:32:47+00:00Added an answer on May 22, 2026 at 10:32 pm

    Since BusManager is a Generic, the type you pass into Of <type> must be specified at compile time. It’s not like a traditional parameter that you can change at runtime.

    It’s unclear from the code you listed what BusManager actually does. If all it’s doing is creating an instance of the Generic type, then maybe the person who created it doesn’t really understand generics. Do you have the ability to rework how BusManager works, or are you limited to using it as is?

    As @jmoreno mentioned, you can use reflection to create an instance of a type from a string containting the name of the type. Here’s how that would work:

    Imports System.Reflection
    Imports System.IO
    
    Public Class ObjectFactory
        Private Shared Function CreateObjectFromAssembly(ByVal assembly As Assembly, ByVal typeName As String) As Object
            ' resolve the type
            Dim targetType As Type = assembly.GetType(typeName)
            If targetType Is Nothing Then
                Throw New ArgumentException("Can't load type " + typeName)
            End If
    
            ' get the default constructor and instantiate
            Dim types(-1) As Type
            Dim info As ConstructorInfo = targetType.GetConstructor(types)
            Dim targetObject As Object = info.Invoke(Nothing)
            If targetObject Is Nothing Then
                Throw New ArgumentException("Can't instantiate type " + typeName)
            End If
    
            Return targetObject
        End Function
    
        Public Shared Function CreateObject(ByVal typeName As String) As Object
            Return CreateObjectFromAssembly(Assembly.GetExecutingAssembly, typeName)
        End Function
    
        Public Shared Function CreateObject(ByVal typeName As String, ByVal assemblyFileName As String) As Object
            Dim assemblyFileInfo = New FileInfo(assemblyFileName)
            If assemblyFileInfo.Exists Then
                Return CreateObjectFromAssembly(Reflection.Assembly.LoadFrom(assemblyFileName), typeName)
            Else
                Throw New ArgumentException(assemblyFileName + " cannot be found.")
            End If
        End Function
    
    End Class
    

    In a production app, I’d probably set the return type for all of these methods to my base class or interface. Just make sure you pass in the full typeName including the Namespace.

    With that factory class in place, then the elegant version of your code would look something like this:

    Dim myItem as DTO.MyBaseClass = ObjectFactory.CreateObject("DTO." & GetTypeString())
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have this code to decode numeric html entities to the UTF8 equivalent character.
In my XML file chapters tag has more chapter tag.i need to display chapters

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.