Let's suppose that the table of
Worker
s is not changing during runtime:
For example you have a collection of
Worker
s
Dim WorkerList As New List(Of Worker)()
Then the application needs to
Select
all
Worker
s from database once at starting.
Now, you need a field:
Private random As New Random()
And a method:
Public Function GetAWorker(workerList As List(Of Worker)) As Worker
Dim myWorker As Worker = workerList(random.[Next](0, workerList.Count()))
workerList.Remove(myWorker)
Return myWorker
End Function
You must pass the
WorkerList
to this method and get a random worker.
It removes the selected worker from the the
WorkerList
because it is
Reference Type
.
So the application can add unused worker to the list without any interfering.
Note:
If the table of workers may be modified during runtime, then have to check the database, and add new workers to
WorkerList
before than calling the method.