diddom 发表于 2012-6-6 07:44:57

Using dynamic arrays

<HTML>
<HEAD>
<TITLE>Dynamic Array Application</TITLE>
<SCRIPT LANGUAGE="vbscript">

Option Explicit   'require all variables to be declared

ReDim myArray(0)    'create a dynamic array with 1 element
Dim intIndex      'variable to track the array index

intIndex = 0      'assign the first index number to counter
   
Sub cmdButton1_OnClick
       
   ' Store the user input in the array
   myArray(intIndex) = Document.frmForm1.txtText1.Value   
   intIndex = intIndex + 1          'increment the array counter by one
   ReDim Preserve myArray(intIndex) 'increase the size of the array
   Document.frmForm1.txtText1.Value = ""   'Empty the text box again
End Sub

Sub cmdButton2_OnClick
   Dim x, y, strArrayContents      'declare some variables we'll need
         
   'repeat this process as many times as there are array elements
   'note: the last element will always be empty because we've
   'incremented the counter *after* the assignment.
   'try changing the above sub so that we always fill every element
   For x = 0 to intIndex - 1
      'assign a short description and the element no to the variable
      strArrayContents = strArrayContents & "Element No." & _
                         CStr(x) & " = "
      'add to this the contents of the element
      strArrayContents =strArrayContents & myArray(x) & vbCRLF
      'go back and do it again for the next value of x
    Next
   'when we're done show the result in a message box
    y = MsgBox(strArrayContents,0,"Dynamic Array Application")
End Sub

</SCRIPT>
</HEAD>
<BODY BGCOLOR="white">
<FORM NAME="frmForm1">
   <INPUT TYPE="text" NAME="txtText1"><BR>
   <INPUT TYPE="button" NAME="cmdButton1" VALUE="Add to array"><P>
   <INPUT TYPE="button" NAME="cmdButton2" VALUE="Show Array Contents">
</FORM>
</BODY>
</HTML>

kk1025 发表于 2013-4-12 22:59:55

M。。原來是這樣做

sku__ 发表于 2015-1-11 14:17:49

谢谢分享
页: [1]
查看完整版本: Using dynamic arrays