vb.net中的随机工作者ID [英] random worker id in vb.net

查看:74
本文介绍了vb.net中的随机工作者ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个编号为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 of Workers is not changing during runtime:
For example you have a collection of Workers
Dim WorkerList As New List(Of Worker)()


然后应用程序需要在启动时从数据库中一次Select所有Worker.
现在,您需要一个字段:


Then the application needs to Select all Workers 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆