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

x
  • 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
             i=i+1;
             sAttrib[i] = sLocalAttr[j];
     ENDIF;
NEXT;
0
AutoMagicly
Asked:
AutoMagicly
1 Solution
 
GilesBathgateCommented:
(are you sure your example is VB it looks like a cross between C and Basic)

Anyway
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
             i=i+1
             Redim Preserve sAttrib(i) '<--------THIS LINE HERE
             sAttrib(i) = sLocalAttr(j)
     End If
Next

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
        a.add(s)
     End If
Next

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

thanks again.

0

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