vb.net中的随机工作者ID [英] random worker id in vb.net
问题描述
嗨
我有一个编号为5的ID作为主键的工人列表.
我的应用程序可以将新工作程序添加到此列表中,而不是让用户
确定要为新工作人员选择的ID,我希望系统选择未使用的随机ID.
有没有一种方法可以在不每次需要加载表的情况下做到这一点?
重要的是,其中的ID必须有5个不以0开头的数字..
有什么建议吗?
thanx
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
推荐答案
让我们假设Worker
s的表在运行时没有变化:
例如,您有一个Worker
s
的集合
Let''s suppose that the table ofWorker
s is not changing during runtime:
For example you have a collection ofWorker
s
Dim WorkerList As New List(Of Worker)()
然后应用程序需要在启动时从数据库中一次Select
所有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
您必须将WorkerList
传递给此方法并获得一个随机工作器.
因为它是Reference Type
,所以它将从WorkerList
中删除选定的工作程序.
因此,应用程序可以将未使用的工作程序添加到列表中,而不会产生任何干扰.
注意:
如果可以在运行时修改工作表,则必须在调用该方法之前检查数据库,并将新工作器添加到WorkerList
.
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.
为什么重要的是什么工作器只要不使用该工作人员,就选择.例如,您能否简单地为未使用的工人使用FIFO列表并选择第一个.
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.
这篇关于vb.net中的随机工作者ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!