Click here to Skip to main content
15,900,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi

I have a list of workers with 5 numbered id as primary key.
my application can add new workers to this list but instead of letting the user
decide what id to pick for the new worker, I want the system to pick a random id that is not used.
Is there a way to do that without loading my table each time I need to do it?
it's important to have an id with 5 numbers in it that doesn't start with 0..

any suggestions?

thanx
Posted

Let's suppose that the table of Workers is not changing during runtime:
For example you have a collection of Workers
VB
Dim WorkerList As New List(Of Worker)()

Then the application needs to Select all Workers from database once at starting.
Now, you need a field:
VB
Private random As New Random()

And a method:
VB
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.
 
Share this answer
 
v2
Comments
danait25 2-Apr-12 1:19am    
thanks. I dont understand how do I get a NEW number id from the list? I have the list(I understood you meant my original list of workers) > I pick a random id> I remove it from the list and then what? I need to create a formula that creates a new id based on the random one?
Shahin Khorshidnia 2-Apr-12 5:43am    
If the workers are stored in a database, then you can easily retrieve them without a 5 digits code. But if you want the workers are shown as codes, I suggest that you store {worker's code in Database} OR {You can covert worker' ID (Primary Key in Table) to a 5 digits number in UI, But I don't recommend the second way, because the workers can not be more than 99999} If I haven't understood you exact intention then please describ it more clear.
Why would it be important what worker is selected as long as the worker is not in use. Could you simply use for example a FIFO list for workers not in use and pick the first one.
 
Share this answer
 
Comments
danait25 2-Apr-12 1:22am    
I thought about it but I have already numbers that start with [988##] etc.. so I don't want my numbers to run too fast (although it's probably Hundreds of ids before it's run out..)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900