'核心函数如下,至于数组叫什么名字或者你怎么用那是你自己的自由了。Option ExplicitDim sysAry() As VariantPrivate Sub Form_Load()
Dim tLoop As Integer
Randomize Timer
ReDim sysAry(0)
For tLoop = 1 To 100
ArraysAddRandom_DeOver sysAry(), 100, 10
Next
ArrayViewToListBox sysAry(), List1
End Sub'显示数组到ListBox。
Function ArrayViewToListBox(pAry() As Variant, pListBox As ListBox)
'显示数组到ListBox。
'参数:pAry() 数组。
' pListBox ListBox对象。
Dim tStartElement As Variant
pListBox.Clear
For Each tStartElement In pAry()
pListBox.AddItem tStartElement
Next
End Function'添加一个随机数到数组,并检查重复。
Function ArraysAddRandom_DeOver(pAry() As Variant, ByVal pValMin As Integer, ByVal pValMax As Integer) As Boolean
'添加一个指定范围的随机数到数组,并检查重复。
'注意:数组前必须是动态数组,且一定要至少有一个元素。否则将出错。
'参数:pAry() 数组
' pValMin 随机数的最小值。
' pValMax 随机数的最大值。
'返回:Boolean 是否有重复现象检测到。
Dim tOutBool As Boolean
Dim tElement As Variant
Dim tValLong As Integer
ParBigRight pValMin, pValMax
tValLong = Abs(pValMax - pValMin)
tElement = Int(Rnd * tValLong) + pValMin
tOutBool = ArraysDeOver(pAry(), tElement)
ArraysAddElement tElement, pAry, -1
ArraysAddRandom_DeOver = tOutBool
End Function'添加数到数组。
Function ArraysAddElement(ByVal pElement As Variant, pAry() As Variant, ByVal pIndex As Integer) As Boolean
'添加数到数组。由pIndex决定位置。如果pIndex是-1则添加到尾部。添加到尾部或pIndex超过数组的下标将同时动态给数组增加一个元素。
'注意:数组前必须是动态数组,且一定要至少有一个元素。否则将出错。
'参数:pElement 要添加或设置的元素值。
' pAry() 数组
' pIndex 在数组中的索引,如果是-1表示在尾部追加。
'返回:Boolean 是否增加了数组的元素。
Dim tOutBool As Boolean
Dim tAryLength As Integer
Dim tAryAddElmement As Boolean
tAryLength = UBound(pAry)
tAryAddElmement = pIndex < 0
tOutBool = tAryAddElmement Or pIndex > tAryLength
If tOutBool Then
If tAryAddElmement Then
tAryLength = tAryLength + 1
Else
tAryLength = pIndex
End If
ReDim Preserve pAry(tAryLength)
pAry(tAryLength) = pElement
Else
pAry(pIndex) = pElement
End If
ArraysAddElement = tOutBool
End Function'检查重复。
Function ArraysDeOver(pAry() As Variant, ByVal pValue As Variant) As Boolean
'检查重复。如果在pAry里发现了pValue的存在,则置该元素的值为-1。
'参数:pAry() 数组
' pValue 检测的值
'返回:Boolean 是否有重复现象检测到。
Dim tOutBool As Boolean
Dim tLoop As Integer
Dim tAryLength As Integer
tAryLength = UBound(pAry)
For tLoop = 0 To tAryLength
tOutBool = (pAry(tLoop) = pValue)
If tOutBool Then pAry(tLoop) = -1
Next
ArraysDeOver = tOutBool
End FunctionFunction ParBigRight(pL As Variant, pR As Variant) As Boolean
'检查参数值的情况。保证pR必须大于pL
Dim tT As Variant
Dim tOutBool As Boolean
tOutBool = pL > pR
If pL > pR Then tT = pL: pL = pR: pR = tT
ParBigRight = tOutBool
End Function
Dim tLoop As Integer
Randomize Timer
ReDim sysAry(0)
For tLoop = 1 To 100
ArraysAddRandom_DeOver sysAry(), 100, 10
Next
ArrayViewToListBox sysAry(), List1
End Sub'显示数组到ListBox。
Function ArrayViewToListBox(pAry() As Variant, pListBox As ListBox)
'显示数组到ListBox。
'参数:pAry() 数组。
' pListBox ListBox对象。
Dim tStartElement As Variant
pListBox.Clear
For Each tStartElement In pAry()
pListBox.AddItem tStartElement
Next
End Function'添加一个随机数到数组,并检查重复。
Function ArraysAddRandom_DeOver(pAry() As Variant, ByVal pValMin As Integer, ByVal pValMax As Integer) As Boolean
'添加一个指定范围的随机数到数组,并检查重复。
'注意:数组前必须是动态数组,且一定要至少有一个元素。否则将出错。
'参数:pAry() 数组
' pValMin 随机数的最小值。
' pValMax 随机数的最大值。
'返回:Boolean 是否有重复现象检测到。
Dim tOutBool As Boolean
Dim tElement As Variant
Dim tValLong As Integer
ParBigRight pValMin, pValMax
tValLong = Abs(pValMax - pValMin)
tElement = Int(Rnd * tValLong) + pValMin
tOutBool = ArraysDeOver(pAry(), tElement)
ArraysAddElement tElement, pAry, -1
ArraysAddRandom_DeOver = tOutBool
End Function'添加数到数组。
Function ArraysAddElement(ByVal pElement As Variant, pAry() As Variant, ByVal pIndex As Integer) As Boolean
'添加数到数组。由pIndex决定位置。如果pIndex是-1则添加到尾部。添加到尾部或pIndex超过数组的下标将同时动态给数组增加一个元素。
'注意:数组前必须是动态数组,且一定要至少有一个元素。否则将出错。
'参数:pElement 要添加或设置的元素值。
' pAry() 数组
' pIndex 在数组中的索引,如果是-1表示在尾部追加。
'返回:Boolean 是否增加了数组的元素。
Dim tOutBool As Boolean
Dim tAryLength As Integer
Dim tAryAddElmement As Boolean
tAryLength = UBound(pAry)
tAryAddElmement = pIndex < 0
tOutBool = tAryAddElmement Or pIndex > tAryLength
If tOutBool Then
If tAryAddElmement Then
tAryLength = tAryLength + 1
Else
tAryLength = pIndex
End If
ReDim Preserve pAry(tAryLength)
pAry(tAryLength) = pElement
Else
pAry(pIndex) = pElement
End If
ArraysAddElement = tOutBool
End Function'检查重复。
Function ArraysDeOver(pAry() As Variant, ByVal pValue As Variant) As Boolean
'检查重复。如果在pAry里发现了pValue的存在,则置该元素的值为-1。
'参数:pAry() 数组
' pValue 检测的值
'返回:Boolean 是否有重复现象检测到。
Dim tOutBool As Boolean
Dim tLoop As Integer
Dim tAryLength As Integer
tAryLength = UBound(pAry)
For tLoop = 0 To tAryLength
tOutBool = (pAry(tLoop) = pValue)
If tOutBool Then pAry(tLoop) = -1
Next
ArraysDeOver = tOutBool
End FunctionFunction ParBigRight(pL As Variant, pR As Variant) As Boolean
'检查参数值的情况。保证pR必须大于pL
Dim tT As Variant
Dim tOutBool As Boolean
tOutBool = pL > pR
If pL > pR Then tT = pL: pL = pR: pR = tT
ParBigRight = tOutBool
End Function
ArraysAddRandom_DeOver LOGS(), 100, 10
End SubPrivate Sub CommandView_Click()
ArrayViewToListBox LOGS(), ListBoxView
End SubPrivate Sub Form_Load()
Randomize Timer
ReDim LOGS(0) ’千万要有这一句,否则有生命危险:)……
End Sub