Arrays 如何在VB.net中生成唯一数数组?
我需要实现一个生成唯一组合号数组的函数? 很多设备都可以访问这个函数(通过使用线程),每个设备都应该获得一个唯一的地址id(sbyte数组) 我以前在C#中使用了一个函数来生成一个唯一的数字,但我不知道如何在VB.net中实现这个新案例Arrays 如何在VB.net中生成唯一数数组?,arrays,vb.net,random,sync,Arrays,Vb.net,Random,Sync,我需要实现一个生成唯一组合号数组的函数? 很多设备都可以访问这个函数(通过使用线程),每个设备都应该获得一个唯一的地址id(sbyte数组) 我以前在C#中使用了一个函数来生成一个唯一的数字,但我不知道如何在VB.net中实现这个新案例 public class GetUniqueNumber { static private int id = 0; static private final Object lock = new Object(); public in
public class GetUniqueNumber
{
static private int id = 0;
static private final Object lock = new Object();
public int getId()
{
synchronized (lock)
{
int temp = id;
id++;
return temp;
}
}
}
像这样
' Define other methods and classes here
public class GetUniqueNumber
shared id as integer=0
shared readonly lock =new Object()
public function getId() as integer
SyncLock lock
dim temp =id
id=id+1
return temp
end synclock
end function
end class
您需要使用
同步锁
,如下所示:
Public Class GetUniqueNumber
Private Shared id As Integer = 0
Private Shared lock = New Object()
Public Function getId() As Integer
SyncLock lock
Dim temp As Integer = id
id += 1
Return temp
End SyncLock
End Function
End Class
作为整数
Public Class GetUniqueNumber
Private Shared id As Integer = 0
Private Shared lock As New Object
Public ReadOnly Property getID() As Integer
Get
Dim temp As Integer
SyncLock lock
temp = id
id += 1
End SyncLock
Return temp
End Get
End Property
End Class
as字节数组
Public Class GetUniqueNumber
Private Shared id As Integer = 0
Private Shared lock As New Object
Public ReadOnly Property getID() As Byte()
Get
Dim temp As Byte()
SyncLock lock
temp = BitConverter.GetBytes(id)
id += 1
End SyncLock
Return temp
End Get
End Property
End Class
作为int16中的字节数组
Public Class GetUniqueNumber
Private Shared id As Int16 = 0
Private Shared lock As New Object
Public ReadOnly Property getID() As Byte()
Get
Dim temp As Byte()
SyncLock lock
temp = BitConverter.GetBytes(id)
id += 1S
End SyncLock
Return temp
End Get
End Property
End Class
使用biginteger
Public Class GetUniqueNumber
Private Shared id As BigInteger = 0
Private Shared lock As New Object
Public ReadOnly Property getID() As Byte()
Get
Dim temp As Byte()
SyncLock lock
temp = id.ToByteArray
id += 1
End SyncLock
If temp.Length <> 16 Then
Array.Resize(temp, 16)
End If
Return temp
End Get
End Property
End Class
是的,这段代码与我发布的C#代码类似,但现在我需要生成一个由唯一组合数组成的数组[16],而不仅仅是一个int值。谢谢,但我忘了指出数组必须有一个固定长度(16)。很抱歉,当我执行测试时,返回的“temp”数组的长度是2。预期的值类似于:Dim temp()as Byte=New Byte(){1,0,0,0,0,0,0,0,0,0,0,0,0}。也许我不是很清楚也许“Redim Preserve temp(15)”应该可以解决这个问题我正在使用框架2.0,所以我没有BigInteger类型。请参阅上次编辑。无法想象需要这么大的数字。
Public Class GetUniqueNumber
Private Shared idLS As Long = 0L
Private Shared idMS As Long = 0L
Private Shared lock As New Object
Public ReadOnly Property getID() As Byte()
Get
Dim tempLS() As Byte
Dim tempMS() As Byte
Dim rv(15) As Byte
SyncLock lock
tempLS = BitConverter.GetBytes(idLS)
tempMS = BitConverter.GetBytes(idMS)
If idLS = Long.MaxValue Then
idMS += 1L
idLS = 0L
Else
idLS += 1L
End If
Array.Reverse(tempLS)
Array.Reverse(tempMS)
Array.Copy(tempLS, 0, rv, 8, tempLS.Length)
Array.Copy(tempMS, 0, rv, 0, tempMS.Length)
End SyncLock
Return rv
End Get
End Property
End Class