50分问一个简单问题

lok9828 2006-01-09 01:33:38

Dim Archives() As String
ReDim Archives(0 To 3)

Archives(0) = "abc"
Archives(1) = "xxx"
Archives(2) = "asdfsd"
Archives(3) = "xxx"
我想删除其中一条,变成如下,应该怎么做.
Archives(0) = "abc"
Archives(1) = "asdfsd"
Archives(2) = "xxx"


我想添加一条数据变成如下,应该怎么做.
Archives(0) = "abc"
Archives(1) = "xxx"
Archives(2) = "加入的数据"
Archives(3) = "asdfsd"
Archives(4) = "xxx"

对vb的数组不是很熟悉,我现在要实现这个功能就是用一个临时数组.不过好像很笨,特请教vb的DX
...全文
123 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
rainstormmaster 2006-01-09
  • 打赏
  • 举报
回复
这么删:
Option Explicit
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (dest As Any, source As Any, ByVal numBytes As Long)
Private Sub DeleteStringItem(strArr() As String, ByVal index As Long)
Dim lastItem As Long, saveAddr As Long
lastItem = UBound(strArr)
saveAddr = StrPtr(strArr(index))
CopyMemory ByVal VarPtr(strArr(index)), ByVal VarPtr(strArr(index + 1)), (lastItem - index) * 4
CopyMemory ByVal VarPtr(strArr(lastItem)), saveAddr, 4
strArr(lastItem) = vbNullString
End Sub

Private Sub Command1_Click()
Dim Archives() As String
ReDim Archives(0 To 3)
Archives(0) = "abc"
Archives(1) = "xxx"
Archives(2) = "asdfsd"
Archives(3) = "xxx"
DeleteStringItem Archives, 1 '删除Archives(1)
'测试输出
Dim i As Long
For i = 0 To UBound(Archives)
Debug.Print Archives(i)
Next
End Sub
rainstormmaster 2006-01-09
  • 打赏
  • 举报
回复
等等
lok9828 2006-01-09
  • 打赏
  • 举报
回复
to: northwolves(狼行天下)

你好 :

感谢你的答案,
不过你删除的是数组的最后一条.我想删除中间的一条.
northwolves 2006-01-09
  • 打赏
  • 举报
回复
添加或删除数组元素用CopyMemory更快一些,如:

Option Explicit
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Dim a() As Byte

Private Sub Command1_Click()
CopyMemory a(51), a(52), 49
ReDim Preserve a(99)
End Sub

Private Sub Form_Load()
ReDim a(100)
Dim i As Long
For i = 0 To 100
a(i) = i
Next
End Sub
northwolves 2006-01-09
  • 打赏
  • 举报
回复
Dim Archives() As String



Private Sub Command1_Click()
ReDim Archives(0 To 3)
Archives(0) = "abc"
Archives(1) = "xxx"
Archives(2) = "asdfsd"
Archives(3) = "xxx"
MsgBox Join(Archives, vbCrLf)
ReDim Preserve Archives(0 To 2)
MsgBox Join(Archives, vbCrLf)
End Sub


Private Sub Command2_Click()
ReDim Archives(0 To 3)
Archives(0) = "abc"
Archives(1) = "xxx"
Archives(2) = "asdfsd"
Archives(3) = "xxx"
MsgBox Join(Archives, vbCrLf)

ReDim Preserve Archives(0 To 4)
For i = 4 To 3 Step -1
Archives(i) = Archives(i - 1)
Next
Archives(2) = "加入的数据"
MsgBox Join(Archives, vbCrLf)
End Sub

7,762

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧