Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Best way to return class at runtime - based on value

Best way to return class at runtime - based on value

Scheduled Pinned Locked Moved Visual Basic
questionjsonhelptutorialdiscussion
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    DeepToot
    wrote on last edited by
    #1

    I have a task that is being repeated throughout the application many times. My goal is to take those methods and consolidate it into a Global method. The problem I am running into is that each task uses a different class type. My thoughts were to create a method, say IncrementLine(), passing in as a parameter an Enum that would tell it what class it is coming from (Rooms or Library for instance). This is an example of the existing code from one of these methods: (Also this is the exact code I need to change to bring back different classes, instead of RoomData it could be LibraryData - instead of Room it could be Library)

    Dim lstRooms As List(Of RoomData) = Room.LoadAllForLibrary(libraryID)

    The code for each of my methods that call IncrementLine() have their own classes like RoomData and Room. With the exception of the code above the rest of the method codes are identical. Without using a bunch of Selects to determine what Class I should use..how can I return the proper class based on the enum passed in? I was thinking of maybe using a Generic Delegate Class..something like this:

    Public Function ReturnClass(Of t)(ByVal _lType As ListType) As t

    End Function

    based on what enum value they place it would send back the correct class..however, once I created this class I realized I'd still have to do a Select to determine what ListType they provided. So my question is, how can I change the code above to be dynamic in returning the proper class based on the enum? Thanks

    A L 2 Replies Last reply
    0
    • D DeepToot

      I have a task that is being repeated throughout the application many times. My goal is to take those methods and consolidate it into a Global method. The problem I am running into is that each task uses a different class type. My thoughts were to create a method, say IncrementLine(), passing in as a parameter an Enum that would tell it what class it is coming from (Rooms or Library for instance). This is an example of the existing code from one of these methods: (Also this is the exact code I need to change to bring back different classes, instead of RoomData it could be LibraryData - instead of Room it could be Library)

      Dim lstRooms As List(Of RoomData) = Room.LoadAllForLibrary(libraryID)

      The code for each of my methods that call IncrementLine() have their own classes like RoomData and Room. With the exception of the code above the rest of the method codes are identical. Without using a bunch of Selects to determine what Class I should use..how can I return the proper class based on the enum passed in? I was thinking of maybe using a Generic Delegate Class..something like this:

      Public Function ReturnClass(Of t)(ByVal _lType As ListType) As t

      End Function

      based on what enum value they place it would send back the correct class..however, once I created this class I realized I'd still have to do a Select to determine what ListType they provided. So my question is, how can I change the code above to be dynamic in returning the proper class based on the enum? Thanks

      A Offline
      A Offline
      Ali Al Omairi Abu AlHassan
      wrote on last edited by
      #2

      sir; i think that you need to use Type.GetConstructor() to return an instance of the appropriate type. Good Luck;

      Help people,so poeple can help you.

      1 Reply Last reply
      0
      • D DeepToot

        I have a task that is being repeated throughout the application many times. My goal is to take those methods and consolidate it into a Global method. The problem I am running into is that each task uses a different class type. My thoughts were to create a method, say IncrementLine(), passing in as a parameter an Enum that would tell it what class it is coming from (Rooms or Library for instance). This is an example of the existing code from one of these methods: (Also this is the exact code I need to change to bring back different classes, instead of RoomData it could be LibraryData - instead of Room it could be Library)

        Dim lstRooms As List(Of RoomData) = Room.LoadAllForLibrary(libraryID)

        The code for each of my methods that call IncrementLine() have their own classes like RoomData and Room. With the exception of the code above the rest of the method codes are identical. Without using a bunch of Selects to determine what Class I should use..how can I return the proper class based on the enum passed in? I was thinking of maybe using a Generic Delegate Class..something like this:

        Public Function ReturnClass(Of t)(ByVal _lType As ListType) As t

        End Function

        based on what enum value they place it would send back the correct class..however, once I created this class I realized I'd still have to do a Select to determine what ListType they provided. So my question is, how can I change the code above to be dynamic in returning the proper class based on the enum? Thanks

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Another alternative would be using the Activator, as described here[^].

        I are Troll :suss:

        A 1 Reply Last reply
        0
        • L Lost User

          Another alternative would be using the Activator, as described here[^].

          I are Troll :suss:

          A Offline
          A Offline
          Ali Al Omairi Abu AlHassan
          wrote on last edited by
          #4

          Good alternative, Sir. :thumbsup: 100 :rose:

          Help people,so poeple can help you.

          D 1 Reply Last reply
          0
          • A Ali Al Omairi Abu AlHassan

            Good alternative, Sir. :thumbsup: 100 :rose:

            Help people,so poeple can help you.

            D Offline
            D Offline
            DeepToot
            wrote on last edited by
            #5

            I agree great solution, thank you both for the time on this.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups