Initiate X Amount of A Variable
Okay so maybe i'm looking at this problem the wrong way and if I am please tell me but here goes.
I have a class lets call it newcycles.vb and that class has multiple events.
When I called in my program I say something like dim cyc1 as newcycles
I set up multiple eventhandlers to handle the events in the class in the program.
But I need to be able to dynamically create as many instances of the class as I need depending on the user. The only way I could think of was copy and paste the each declaration and event handler x amount of times. That seems ridicoulous and by copy and开发者_运维技巧 paste I mean like
Dim cy1 as newcycles dim cy2 as newcycles dim cyc3 as newcycles etc etc then the event handles Public event bla handles cy1.bla Public event bla2 handles cy2.bla
Is there a better way to do this? Oh and I'm doing this in vb.net.
You could use a generic list and a loop to store all your class initiations
Dim NoOfUsers As Integer = 10
Dim ClassList As List(Of newcycles) = New List(Of newcycles)(NoOfUsers)
For I As Integer = 1 To NoOfUsers
Dim c As newcycles = New newcycles()
c.ObjectIndex = I 'Property for storing the Object Index
AddHandler c.bla, AddressOf bla
ClassList.Add(c)
Next
' The event handler
Private Sub bla(ByVal sender As Object, ByVal e As EventArgs)
Dim c As newcycles = CType(sender, newcycles)
'Do something with c.ObjectIndex which identifies this object in particular
End Sub
And instead of creating multiple event handlers for each object you could handle events of all the objects in a single event handler. To differentiate one object from another you could use the objects unique identifier of some sorts.
Assuming that the same procedure handles all of the events you can do the following. Declare the event in newcycles, then do something like this...
Dim lstCycles As New List(of newcycles)
For X = 0 to 10
lstCycles.Add(New newcycle)
AddHandler lstCycles(X).YourEvent, AddressOf SubThatHandlesThisEvent
Next
Then to access...
For Each objCycle as newcycles In lstCycles
objCycle.SomeBooleanProperty = True
Next
How about creating a collection of NewCycle
objects to which you can dynamically add new items, modify any of the existing items, and remove any of the existing items from code while your application is running? You easily implement this using a List(Of T)
.
Just change your variable declaration to:
Public cycles As List(Of NewCycle)
The documentation for the List(Of T)
class contains a fairly comprehensive example of how to use this collection, and lists the methods you'll need to use in order to manipulate the objects in the list.
If you need to handle events for each of the items in the class, you could consider creating your own custom collection and overriding the Add
and Remove
methods to dynamically add and remove event handlers for the events raised by that object. In other words, each time a NewCycle
item is added to your cycle collection, you can add an event handler for its bla
method using AddHandler
, and each time an item is removed, you can remove the event handler for its bla
method using RemoveHandler
. All of the objects' events can be handled by a single event handler method, rather than having one for each object. See the answers to this question for more on how to do that.
精彩评论