NeuralJack
Centurion
- Joined
- Jul 28, 2005
- Messages
- 138
I use Lists from System.Collections.Generic constantly. Love em, love em, love em.
Here's my question though. I would like to broaden some functions to work on many different lists that I have. I usually avoid using variables defined initally as 'Objects' just because it seems safer to use variables defined as the type of object that you're going to use. But, for this, it looks like defining an object and assigning the proper List to it might be the only way to accomplish what I want.
My guess is that using the Object variable really shouldnt be bad as long as I only use functions that the List object has. I'm trying to cut down on code, but not at the cost of stability. Let me know what you think.
Of course, my functions are much bigger than DeleteLastEntryOfChosenList and so the code consolidation would be worth it.
Edit: The other thing I'll do is add a verification procedure that will check if the object SelectedList is one of the many valid types of Lists before proceeding. If i do that there really should be no harm in doing it this way, I'm guessing.
Here's my question though. I would like to broaden some functions to work on many different lists that I have. I usually avoid using variables defined initally as 'Objects' just because it seems safer to use variables defined as the type of object that you're going to use. But, for this, it looks like defining an object and assigning the proper List to it might be the only way to accomplish what I want.
My guess is that using the Object variable really shouldnt be bad as long as I only use functions that the List object has. I'm trying to cut down on code, but not at the cost of stability. Let me know what you think.
Code:
Dim List1 as New List(of Class1)
Dim List2 as New List(of Class2)
Dim List3 as New List(of Class3)
Dim ListChoice as Integer = 1
Private Sub DeleteLastEntryOfChosenList()
'Select Proper List
Dim SelectedList as New Object
If ListChoice = 1 Then
SelectedList = List1
Elseif ListChoice = 2 Then
SelectedList = List2
Elseif ListChoice = 3 Then
SelectedList = List3
Else
SelectedList = Nothing
EndIf
If SelectedList IsNot Nothing Then
If SelectedList.Count > 0
SelectedList.RemoveAt(SelectedList.Count - 1)
EndIf
EndIf
End Sub
Of course, my functions are much bigger than DeleteLastEntryOfChosenList and so the code consolidation would be worth it.
Edit: The other thing I'll do is add a verification procedure that will check if the object SelectedList is one of the many valid types of Lists before proceeding. If i do that there really should be no harm in doing it this way, I'm guessing.
Last edited: