[原创]快速删除数组中对应索引项解决方法
[原创]快速删除数组中对应索引项
相信大家都知道在VB中是不提供直接删除数组中某个元素的函数的,这样就得自己编写函数,很多朋友都是使用循环的方式来实现的,这样效率太低了,刚好这几天刚到新公司上班,还没安排任务下来,无聊就写了这个程序主要是为了在VB中提高效率,程序运行一次没问题,但是郁闷的是运行第二次会出问题,由于上班时间没得多的调试时间。大家可以一起研究改善一下。和文章“快速复制数组”------http://community.csdn.net/Expert/topic/5461/5461836.xml?temp=.1506464出现的问题差不多,希望感兴趣有时间的朋友看看把问题找到好方便大家。
Option Explicit
Private Declare Sub CopyMemory Lib "kernel32 " Alias "RtlMoveMemory " (Destination As Any, Source As Any, ByVal Length As Long)
Private strSave() As String
Private strView() As String
Private Sub cmdDelete_Click()
Dim lng1 As Long, lng2 As Long
' lng1 = VarPtr(strSave(0))
' CopyMemory lng2, lng1, 4
' ReDim strView(UBound(strSave) - 1)
' CopyMemory ByVal VarPtr(strView(0)), ByVal lng2, 4 * (CInt(textIndex.Text))
' lng1 = VarPtr(strSave(CInt(textIndex.Text) + 1))
' CopyMemory lng2, lng1, 4
' CopyMemory ByVal VarPtr(strView(textIndex.Text)), ByVal lng2, 4 * (UBound(strSave) - CInt(textIndex.Text) + 1)
Call DeleteArrayDataByIndex(strSave, CInt(textIndex.Text))
End Sub
Private Sub cmdExit_Click()
Unload Me
End Sub
Private Sub cmdSave_Click()
cmdSave.Enabled = False
strSave = Split(textSave.Text, ", ")
cmdSave.Enabled = True
Randomize
textIndex.Text = CStr(Int((UBound(strSave) * Rnd) + 1))
End Sub
Private Sub cmdView_Click()
Dim strTmp As String, i As Integer
For i = LBound(strView) To UBound(strView)
strTmp = strTmp & strView(i) & ", "
Next
textView.Text = Left(strTmp, Len(strTmp) - 1)
End Sub
Private Sub CopyStringArray(strSourceArray() As String, strDestArray() As String)
Dim lng1 As Long, lng2 As Long
If IsStringArrayIsInitialize(strSourceArray) Then
lng1 = VarPtr(strSourceArray(0))
CopyMemory lng2, lng1, 4
ReDim strDestArray(UBound(strSourceArray))
CopyMemory ByVal VarPtr(strDestArray(0)), ByVal lng2, 4 * (UBound(strSourceArray) + 1) 'LenB(strSourceArray(0)) * (UBound(strSourceArray) + 1)
相信大家都知道在VB中是不提供直接删除数组中某个元素的函数的,这样就得自己编写函数,很多朋友都是使用循环的方式来实现的,这样效率太低了,刚好这几天刚到新公司上班,还没安排任务下来,无聊就写了这个程序主要是为了在VB中提高效率,程序运行一次没问题,但是郁闷的是运行第二次会出问题,由于上班时间没得多的调试时间。大家可以一起研究改善一下。和文章“快速复制数组”------http://community.csdn.net/Expert/topic/5461/5461836.xml?temp=.1506464出现的问题差不多,希望感兴趣有时间的朋友看看把问题找到好方便大家。
Option Explicit
Private Declare Sub CopyMemory Lib "kernel32 " Alias "RtlMoveMemory " (Destination As Any, Source As Any, ByVal Length As Long)
Private strSave() As String
Private strView() As String
Private Sub cmdDelete_Click()
Dim lng1 As Long, lng2 As Long
' lng1 = VarPtr(strSave(0))
' CopyMemory lng2, lng1, 4
' ReDim strView(UBound(strSave) - 1)
' CopyMemory ByVal VarPtr(strView(0)), ByVal lng2, 4 * (CInt(textIndex.Text))
' lng1 = VarPtr(strSave(CInt(textIndex.Text) + 1))
' CopyMemory lng2, lng1, 4
' CopyMemory ByVal VarPtr(strView(textIndex.Text)), ByVal lng2, 4 * (UBound(strSave) - CInt(textIndex.Text) + 1)
Call DeleteArrayDataByIndex(strSave, CInt(textIndex.Text))
End Sub
Private Sub cmdExit_Click()
Unload Me
End Sub
Private Sub cmdSave_Click()
cmdSave.Enabled = False
strSave = Split(textSave.Text, ", ")
cmdSave.Enabled = True
Randomize
textIndex.Text = CStr(Int((UBound(strSave) * Rnd) + 1))
End Sub
Private Sub cmdView_Click()
Dim strTmp As String, i As Integer
For i = LBound(strView) To UBound(strView)
strTmp = strTmp & strView(i) & ", "
Next
textView.Text = Left(strTmp, Len(strTmp) - 1)
End Sub
Private Sub CopyStringArray(strSourceArray() As String, strDestArray() As String)
Dim lng1 As Long, lng2 As Long
If IsStringArrayIsInitialize(strSourceArray) Then
lng1 = VarPtr(strSourceArray(0))
CopyMemory lng2, lng1, 4
ReDim strDestArray(UBound(strSourceArray))
CopyMemory ByVal VarPtr(strDestArray(0)), ByVal lng2, 4 * (UBound(strSourceArray) + 1) 'LenB(strSourceArray(0)) * (UBound(strSourceArray) + 1)