Announcement

Collapse
No announcement yet.

getting ubound of user-defined arrays

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • getting ubound of user-defined arrays

    I have data type like this:

    Code:
    Type csaType
         cLine As String * 75
         cSampleID As String * 75
         cAggl As String * 10
         cMot As String * 10
         cVol As String * 10
         cConc As String * 10
         cTSD As String * 10
         cTar As String * 10
         cTech As String * 75
    End Type
    then I dimension an array

    Code:
    dim csaTable() as csaType
    I have several arrays like this in my program.
    I was using UBOUND(csaTable)
    and it was working, then suddenly on one array it didn't work.
    I looked at PB help and I see
    UBOUND cannot be used on arrays within User-Defined Types.

    It *kind* of works... but if not, how can I get the ubound of a typed array?


  • #2
    You have an array of a user defined type, not an array in a user defined type.

    (added - In Help for TYPE/END TYPE, see subscripts section for arrays in a UDT.)

    UBOUND should work. (except that in your DIM statement you do not specify the number of elements in the array.
    example:
    dim csaTable(9) as csaType
    or
    dim csaTable(1 to 10) as csaType
    (both dim with 10 elements)

    Cheers,
    Dale

    Comment


    • #3
      Originally posted by Shawn Anderson View Post
      I have data type like this:

      It *kind* of works... but if not, how can I get the ubound of a typed array?
      As I understand the documentation:

      Code:
      function PBmain() as long
      type MyDataType
        FirstName  as Asciiz * 20
        LastName as Asciiz * 50
        KnownAliases( 1 to 10) as AsciiZ * 20
      end type
      
      local MyData() as MyDataType
      
      dim MyData(1 to 10)
      
      'This is legal
      ? ubound(MyData)
      
      'This is not
      ? UBOUND(MyData.KnownAliases)
      
      end function

      Real programmers use a magnetized needle and a steady hand

      Comment


      • #4
        As others have pointed out, UBOUND works for an array of UDTs. Your problem with that one array clearly lies elsewhere.

        "then I dimension an array
        dim csaTable() as csaType"


        That is not dimensioning the array, it is merely declaring it as an un-dimensioned array.

        Do you perhaps get -1 for that array's UBOUND() ?

        Comment


        • #5
          Do you perhaps get -1 for that array's UBOUND() ?
          I do get -1, but I have populated the array.
          As long as I know ubound works for structured arrays, I'll check my code.

          thank you

          Comment


          • #6
            . . . but I have populated the array.
            Not possible!
            Dale

            Comment


            • #7
              Originally posted by Shawn Anderson View Post

              I do get -1, but I have populated the array.
              As long as I know ubound works for structured arrays, I'll check my code.

              thank you
              As Dale says, "not possible"

              Do you have #DIM ALL ? If not, it could be a spelling discrepancy with the array name - regardless, you should always use it.

              Do you have #DEBUG DISPLAY ON / #DEBUG ERROR ON to trap array out of bounds errors? - it pays to have both of these on while developing (comment them out for final tested code with appropriate error trapping)

              Do you have the same array name declared both as global and also in a sub/function?

              Comment


              • #8
                As I said when support for
                Code:
                 TYPE ...
                      name (number)
                  END TYPE
                .. was introduced and referred to in the doc as "an array:"

                These should not be called "arrays" because none of the array statements ( UBOUND(), SORT, SCAN, REDIM, etc) work on them. They should be called "tables" or there is going to be a lot of confusion moving into the future.
                Last edited by Michael Mattias; 17 Sep 2020, 07:06 PM. Reason: Correct typo "SACN" to correct spelling "SCAN"
                Michael Mattias
                Tal Systems Inc. (retired)
                Racine WI USA
                [email protected]
                http://www.talsystems.com

                Comment

                Working...
                X