Advertisement
calfred2808

Multithreaded Array

Jul 29th, 2017
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 1.30 KB | None | 0 0
  1. Multithreaded Array
  2. Typically, when you want to assign names dynamically to objects, it's best to use a dictionary. In the dictionary, you would use the key for the dynamically assigned name and you would use the value as the object that is assigned that name. For instance:
  3.  
  4.  
  5.  
  6. Dim d As New Dictionary(Of String, MyClass)()
  7.  
  8. 'Add objects to dictionary
  9. d("dynamic name 1") = New MyClass()
  10. d("dynamic name 2") = New MyClass()
  11.  
  12. 'Get object from dictionary by name
  13. Dim myObject As MyClass = d("dynamic name 1")
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20.  
  21. The same method will work well with threads, for instance:
  22.  
  23. Dim threads As New Dictionary(Of String, Thread)()
  24. Dim variableName = "Thread"
  25. For i As Integer = 0 To 5
  26.     threads(variableName & "(" & i.ToString() & ")") = New Thread()
  27. Next
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36. However, if all you are doing is assigning numeric indexes to them, rather than string names, you could just use a list instead, like this:
  37.  
  38. Dim threads As New List(Of Thread)()
  39. For i As Integer = 0 To 5
  40.     threads.Add(New Thread())
  41. Next
  42.  
  43.  
  44.  
  45.  
  46.  
  47. Then you could get the thread from the list by index like this:
  48.  
  49. Dim t As Thread = threads(1)
  50. Or, if you have a set number of threads, you could easily use an array:
  51.  
  52. Dim threads(4) As Thread
  53. For i As Integer = 0 To 5
  54.     threads(i) = New Thread()
  55. Next
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement