Return Array Upper Bound

spfeif
2008-06-03
2008-06-06
  • spfeif - 2008-06-03

    I thought I have done this in the past but can't find the code. Is there a function to return the upper bound to an array dynamically? For instance

    MyArray :Array[0..8] of int;

    for i := 0 to MAX(MyArray) do

    something

    end_for

    As long as it returns either the number of elements or the Max index.

     
  • Ralph Holz - 2008-06-06

    Hi Steve,

    No there is no way to do that.

    I always use constants to declare arrays and by doing that i get the bounds of the array.

    VAR
    MyArray :Array[0..8] of int;
    END_VAR
    VAR CONSTANT
    UpperBound: INT:= 8;
    END_VAR
    for i := 0 to UpperBound do
    something
    end_for 
    

    regards

    Ralph

     
  • spfeif - 2008-06-06

    Thanks,

    That is how I solved it as well. My situation was I have about 10- 15 small functions that compared and retrieved information out of an array of structures. In the event that I need to shrink or expand the size I didn't want to change 10-15 functions. So I just used a global array index as you noted and all is good.

    Although I will try it can you use a constant to declare an array size?

    In other words

    Var constant
    Β ARRAY_INDEX :INT := 8;
    end_var
    var
    Β MyArray [1..ARARY_INDEX] of int;
    end_var
    
     
  • Ralph Holz - 2008-06-06

    Ups sorry, thats exacly what i meant!

    Yes you can use constants in declaration to define the array bounds.

    VAR
    MyArray :Array[0..UpperBound] of int;
    END_VAR
    VAR CONSTANT
    UpperBound: INT:= 8;
    END_VAR
    for i := 0 to UpperBound do
    something
    end_for
    

    regards

    Ralph

     

Log in to post a comment.