Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 263
  • Last Modified:

How to fill an array - Index is outside the bounds of the array Need to dynamicly change array bounds

I have an application that uses .NET scripting or a slight variant from that,
I am trying to read values from a system array "me._Attributes[]" into a local array sLocalAttr[]
I need to pick out all values that end in "InAlarm"
Then I would like to fill the second array "sAttrib[] with the values meeting the "InAlarm" criteria.
The issue I am having is with the second array bounds.
I want the sAttrib array bounds to only be as large as required,

when run, the error log says -> Index was outside the bounds of the array.
Please help.

DIM sAttrib[1] AS String;
DIM sLocalAttr[1] AS String;

sLocalAttr[] = me._Attributes[] ; 'Create localized array for attribute variables

FOR j = 1 TO sLocalAttr[].length ' Iterate through all attributes
     IF sLocalAttr[j].EndsWith("InAlarm") == TRUE THEN
             sAttrib[i] = sLocalAttr[j];
1 Solution
(are you sure your example is VB it looks like a cross between C and Basic)

You need to Dimention your array to be big enough to hold the values you are putting into it.

Dim sAttrib() As String
Dim sLocalAttr() As String

sLocalAttr() = me._Attributes()  'Create localized array for attribute variables

For j = 1 To sLocalAttr().length ' Iterate through all attributes
     If sLocalAttr(j).EndsWith("InAlarm") = True Then
             Redim Preserve sAttrib(i) '<--------THIS LINE HERE
             sAttrib(i) = sLocalAttr(j)
     End If

Alternately use an arrayList, and some other fancy language features:
Dim a As ArrayList
Dim sAttrib() as String

For Each sLocalAttr As String in me._Attributes
     If sLocalAttr.EndsWith("InAlarm") Then
     End If

sAttrib = a.ToArray
AutoMagiclyAuthor Commented:
Thanks for the help on this, your example using ArrayList works great,

thanks again.


Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now