如何用数据填充数组?

时间:2015-05-25 18:42:14

标签: arrays vb6

我正致力于使用另一个阵列填充一个阵列。对于不安的事情,我试图从第一个数组中的某个点开始填充。我使用的是Vb6

1 个答案:

答案 0 :(得分:0)

尝试以下示例项目:

Option Explicit

Private Sub Form_Click()
  Dim intIndex As Integer
  Dim intSrc(10) As Integer
  Dim intResult() As Integer
  'fill source array with some etxt values
  For intIndex = 0 To UBound(intSrc)
    intSrc(intIndex) = intIndex * intIndex
  Next intIndex
  'fill resulting array with subset 3 to 7
  intResult = FromArray(intSrc, 3, 7)
  'show resulting array
  For intIndex = 0 To UBound(intResult)
    Print CStr(intIndex) & " : " & CStr(intResult(intIndex))
  Next intIndex
End Sub

Private Function FromArray(intSrc() As Integer, intFrom As Integer, intTo As Integer) As Integer()
  Dim intIndex As Integer
  Dim intResult() As Integer
  'resize resulting array to appropriate size
  ReDim intResult(intTo - intFrom) As Integer
  'fill resulting array with values from source array
  For intIndex = 0 To UBound(intResult)
    intResult(intIndex) = intSrc(intFrom + intIndex)
  Next intIndex
  'return resulting array
  FromArray = intResult
End Function

当您运行它并单击表单时,您将看到生成的数组。

该项目包含一些基本的数组处理函数。尝试一些不同的值来看它是如何工作的。

还要尝试一些奇怪的值,例如intResult = FromArray(intSrc, 7, 3),并尝试理解错误。