Best way to return class at runtime - based on value
-
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
-
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
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.
-
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
-
Good alternative, Sir. :thumbsup: 100 :rose:
Help people,so poeple can help you.
-
Good alternative, Sir. :thumbsup: 100 :rose:
Help people,so poeple can help you.