Altering Size of Arrays

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • TNT
    New Member
    • Feb 2007
    • 48

    Altering Size of Arrays

    How would I alter the size of an array at run-time? I want to read the list of processes and stick each one into a slot in an array, but the number of processes can change alot.
  • willakawill
    Top Contributor
    • Oct 2006
    • 1646

    #2
    Originally posted by TNT
    How would I alter the size of an array at run-time? I want to read the list of processes and stick each one into a slot in an array, but the number of processes can change alot.
    Take a look at ReDim

    Comment

    • vijaydiwakar
      Contributor
      • Feb 2007
      • 579

      #3
      Originally posted by willakawill
      Take a look at ReDim
      If u r using redim then must have a lok on preserve kwd also
      dim myArray() as string
      myarray(0)="XYZ "
      Redim Preserve myArray(10)

      Comment

      • willakawill
        Top Contributor
        • Oct 2006
        • 1646

        #4
        Originally posted by vijaydiwakar
        If u r using redim then must have a lok on preserve kwd also
        dim myArray() as string
        myarray(0)="XYZ "
        Redim Preserve myArray(10)
        This is not normally correct. The preserve version is only used if you want to increase the size of the array while retaining the original contents. It is not used if you simply want to dynamically size the array at run time.

        Comment

        • vijaydiwakar
          Contributor
          • Feb 2007
          • 579

          #5
          Originally posted by willakawill
          This is not normally correct. The preserve version is only used if you want to increase the size of the array while retaining the original contents. It is not used if you simply want to dynamically size the array at run time.
          I've just given the idea

          Comment

          • willakawill
            Top Contributor
            • Oct 2006
            • 1646

            #6
            Originally posted by vijaydiwakar
            I've just given the idea
            Not just the idea.
            This code will not work. You will get a 'subscript out of range' error
            Code:
            dim myArray() as string
            myarray(0)="XYZ"

            Comment

            • TNT
              New Member
              • Feb 2007
              • 48

              #7
              So what code will work?

              Comment

              • willakawill
                Top Contributor
                • Oct 2006
                • 1646

                #8
                Originally posted by TNT
                So what code will work?
                When you are going to dynamically size an array you would follow this pattern:
                Code:
                Dim ar() As Integer
                Dim size As Integer
                
                size = 25
                ReDim ar(size)
                If you already have an array with data stored in it and you wish to resize it to accept more data:
                Code:
                size = 35
                ReDim Preserve ar(size)

                Comment

                Working...