7
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ZOZOテクノロジーズ #3Advent Calendar 2019

Day 5

VBScriptでのReDimの罠

Last updated at Posted at 2019-12-04

VBScriptで動的配列の要素数を変えるためには、ReDimを使います。
しかし、ReDimを使うときに意図せずに配列の中身をすべて消すことになることもあるので、注意が必要です。
例えば、以下のコードではReDimのタイミングで要素数6の空配列になってしまいます。

Dim arr()
ReDim arr(2)
arr(0) = "hoge"
arr(1) = "fuga"
arr(2) = "piyo"

ReDim arr(5)

ReDim以前に配列に保存した値を保持したい時にはPreserveを付与する必要があります。

Dim arr()
ReDim arr(2)
arr(0) = "hoge"
arr(1) = "fuga"
arr(2) = "piyo"

ReDim Preserve arr(5)
7
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?