Home > Articles

  • Print
  • + Share This

Resizing Arrays

If you want to resize an array, you use the ReDim <keyword>. Please be advised that you can resize an array only if it has been created without declaring any elements in the array.

If you find that you need to continually re-dimension a given array, you use the Preserve <keyword> to keep the data in the array intact. If you do not use the Preserve <keyword>, all the data in the array will be removed.

Listing 3.7 shows you how to create an array, Stooges, resize it to accommodate three elements, then resize it again to accommodate an added element, while maintaining the data associated with the three previous elements.

Listing 3.7: Using the ReDim Statement (03asp07.htm)

<SCRIPT LANGUAGE=VBScript>
  Sub button1_OnClick()
    Dim Stooges()
    Dim i
    Dim OutStr

    Redim Preserve Stooges(2)

    Stooges(0) = "Moe"
    Stooges(1) = "Larry"
    Stooges(2) = "Curly"

    Redim Preserve Stooges(3)
    Stooges(3) = "Shemp"

    For i = 0 to UBound(Stooges)
      OutStr = OutStr + ": " + Stooges(i)
      text1.value = OutStr
    Next
  End Sub
</SCRIPT>

Figure 3.6 shows the output from Listing 3.7.

Figure 3.6 The output from a re-dimensioned array.

Now that you have an understanding as to how to declare variables and arrays, let's learn how to use them to make statements.

A statement, also known as an expression, is an instruction that you create using VBScript, or any other language for that matter. You can create statements that assign a value to a variable. You can create statements that perform mathematical operations using variables. You can create statements that execute other statements, and so forth.

VBScript requires that a complete statement be written on a single line. If you do need to extend a statement over multiple lines, you can use the underscore character _ , which VBScript interprets as the line continuation character. For example, both of the statements in Listing 3.8 are valid.

Listing 3.8: The Line Continuation Character _ (03asp08.htm)

<SCRIPT LANGUAGE=VBScript>
  Dim y
  Dim s
  s = (y + y) + (y + 1 + y + 1) + (y + 2 + y + 2)

  s = (y + y) + (y + 1 + y + 1) + _
          (y + 2 + y + 2)
  Alert s
</SCRIPT>

Caution

You cannot use the line continuation character to break up a string within the quotation marks.

This will error:

<SCRIPT LANGUAGE=VBScript>
  Dim s
  s = "I am an erroneously _
    extended string"
</SCRIPT>

If you want to extend a string over two lines, you must truncate the string first, as follows:

<SCRIPT LANGUAGE=VBScript>
  Dim s
  s = "I am a properly" & _
    "extended string"
</SCRIPT>

Using Math Operators

VBScript supports the usual math operators. Table 3.2 shows you the math operators, with an example for each.

Table 3.2: VBScript Simple Math Operators

Operator

Description

Example

+

Addition

t = r + s

-

Subtraction

t = r -s

*

Multiplication

t = r * s

/

Floating point division

t = r / s

^

Exponent

t = 2 ^ 3

(t evaluates to 8)

mod

Remainder

t = 3 mod 1

-(t evaluates to 1, the remainder of 3 divided by 2)


  • + Share This
  • 🔖 Save To Your Account

Related Resources

There are currently no related titles. Please check back later.