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

Posted on 2006-05-20
Last Modified: 2008-03-17

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];
Question by:AutoMagicly
    LVL 3

    Accepted 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

    Author Comment

    Thanks for the help on this, your example using ArrayList works great,

    thanks again.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
    Article by: jpaulino
    XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String ( Literal, only instead of starting and ending with w…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now