error in dynamic array

error in dynamic array

Anonymous
Not applicable
941 Views
8 Replies
Message 1 of 9

error in dynamic array

Anonymous
Not applicable
hi,
i want to make 2-dim array from a selection set.but number of objects varies.so i tried to make dynamic array.but it gives "subscript out of range" errori dont know much bout arrays.pl help.

Dim adBlockAttributes As Variant
Dim adBlock As AcadBlockReference
Dim MyblkArray() As Variant
Dim Index As Integer
Dim x As Integer

x = -1

For Index = 0 To SSdoorblock.Count - 1
Set adBlock = SSdoorblock.Item(Index)
adBlockAttributes = adBlock.GetAttributes

Dim SizeUnit As Double
SizeUnit = ThisDrawing.Utility.DistanceToReal(adBlockAttributes(0).TextString, acDefaultUnits)

x = x + 1
MyblkArray(x, 0) = SizeUnit------------error
MyblkArray(x, 1) = adBlock.ObjectID
Next Index
0 Likes
942 Views
8 Replies
Replies (8)
Message 2 of 9

dgorsman
Consultant
Consultant
I don't see the array being assigned a size. You can add more to, or truncate from, the last dim of the array via redim - but its still sized manually.
----------------------------------
If you are going to fly by the seat of your pants, expect friction burns.
"I don't know" is the beginning of knowledge, not the end.


0 Likes
Message 3 of 9

Anonymous
Not applicable
thanx.
can anybody pl give me an example or hint?how do i redim?say i have 6 blocks.
thank u
0 Likes
Message 4 of 9

dgorsman
Consultant
Consultant
Check our old friend, the VBA help file, it outlines the Redim function fairly well (better than I can mangle, anyways).
----------------------------------
If you are going to fly by the seat of your pants, expect friction burns.
"I don't know" is the beginning of knowledge, not the end.


0 Likes
Message 5 of 9

Anonymous
Not applicable
~snip~
x = -1
Redim MyblkArray (0 to SSdoorblock.Count - 1, 0 to 1)
For Index = 0 To SSdoorblock.Count - 1
~snip~

Enjoy.

See help file

Message was edited by: rstrandmark Message was edited by: Discussion Admin
0 Likes
Message 6 of 9

Anonymous
Not applicable
Below is the help menu snip! Seems like
a great response that the OP should be
thankful for - or just wait around for help...


ReDim Statement
Used at procedure level to reallocate storage space for dynamic array
variables.

Syntax

ReDim [Preserve] varname(subscripts) [As type] [, varname(subscripts) [As
type]] . . .

The ReDim statement syntax has these parts:

Part Description
Preserve Optional. Keyword used to preserve the data in an existing
array when you change the size of the last dimension.
varname Required. Name of the variable; follows standard variable
naming conventions.
subscripts Required. Dimensions of an array variable; up to 60
multiple dimensions may be declared. The subscripts argument uses the
following syntax:
[lower To] upper [,[lower To] upper] . . .

When not explicitly stated in lower, the lower bound of an array is
controlled by the Option Base statement. The lower bound is zero if no
Option Base statement is present.

type Optional. Data type of the variable; may be Byte, Boolean,
Integer, Long, Currency, Single, Double, Decimal (not currently supported),
Date, String (for variable-length strings), String * length (for
fixed-length strings), Object, Variant, a user-defined type, or an object
type. Use a separate As type clause for each variable being defined. For a
Variant containing an array, type describes the type of each element of the
array, but doesn't change the Variant to some other type.



Remarks

The ReDim statement is used to size or resize a dynamic array that has
already been formally declared using a Private, Public, or Dim statement
with empty parentheses (without dimension subscripts).

You can use the ReDim statement repeatedly to change the number of elements
and dimensions in an array. However, you can't declare an array of one data
type and later use ReDim to change the array to another data type, unless
the array is contained in a Variant. If the array is contained in a Variant,
the type of the elements can be changed using an As type clause, unless you're
using the Preserve keyword, in which case, no changes of data type are
permitted.

If you use the Preserve keyword, you can resize only the last array
dimension and you can't change the number of dimensions at all. For example,
if your array has only one dimension, you can resize that dimension because
it is the last and only dimension. However, if your array has two or more
dimensions, you can change the size of only the last dimension and still
preserve the contents of the array. The following example shows how you can
increase the size of the last dimension of a dynamic array without erasing
any existing data contained in the array.

ReDim X(10, 10, 10)
. . .
ReDim Preserve X(10, 10, 15)
Similarly, when you use Preserve, you can change the size of the array only
by changing the upper bound; changing the lower bound causes an error.

If you make an array smaller than it was, data in the eliminated elements
will be lost. If you pass an array to a procedure by reference, you can't
redimension the array within the procedure.

When variables are initialized, a numeric variable is initialized to 0, a
variable-length string is initialized to a zero-length string (""), and a
fixed-length string is filled with zeros. Variant variables are initialized
to Empty. Each element of a user-defined type variable is initialized as if
it were a separate variable. A variable that refers to an object must be
assigned an existing object using the Set statement before it can be used.
Until it is assigned an object, the declared object variable has the special
value Nothing, which indicates that it doesn't refer to any particular
instance of an object.

Caution The ReDim statement acts as a declarative statement if the
variable it declares doesn't exist at module level or procedure level. If
another variable with the same name is created later, even in a wider scope,
ReDim will refer to the later variable and won't necessarily cause a
compilation error, even if Option Explicit is in effect. To avoid such
conflicts, ReDim should not be used as a declarative statement, but simply
for redimensioning arrays.

Note To resize an array contained in a Variant, you must explicitly
declare the Variant variable before attempting to resize its array.

ReDim Statement Example
This example uses the ReDim statement to allocate and reallocate storage
space for dynamic-array variables. It assumes the Option Base is 1.

Dim MyArray() As Integer ' Declare dynamic array.
Redim MyArray(5) ' Allocate 5 elements.
For I = 1 To 5 ' Loop 5 times.
MyArray(I) = I ' Initialize array.
Next I
The next statement resizes the array and erases the elements.

Redim MyArray(10) ' Resize to 10 elements.
For I = 1 To 10 ' Loop 10 times.
MyArray(I) = I ' Initialize array.
Next I
The following statement resizes the array but does not erase elements.

Redim Preserve MyArray(15) ' Resize to 15 elements.



wrote in message news:5643701@discussion.autodesk.com...
~snip~
x = -1
Redim MyblkArray (0 to SSdoorblock.Count - 1, 0 to 1)
For Index = 0 To SSdoorblock.Count - 1
~snip~

Enjoy.

See help file~ wtf matter with you guys?

Message was edited by: rstrandmark Message was edited by: Discussion Admin
0 Likes
Message 7 of 9

dgorsman
Consultant
Consultant
Standard operating procedure for me. I'd rather people learn how to find stuff on their own, because somebody more knowledgeable is not always available. In the process of looking for that information, more information may be discovered that will also be useful. Also, I don't memorize the entire VBA nomenclature so rather than trying to recall possibly faulty information, or look it up and copy it (which anybody can do), I simply provide a pointer to the required data.

Its also a good programming practice to get into. Instead of providing multiple copies of data that may get unsynchronized, put the data in a single place and provide references or pointers to it so any time its needed the same information will result. Thats a little far-fetched metaphor, but its late on a Friday...
----------------------------------
If you are going to fly by the seat of your pants, expect friction burns.
"I don't know" is the beginning of knowledge, not the end.


0 Likes
Message 8 of 9

Anonymous
Not applicable
Kudos! Amazing how much one can learn plowing through the help
menu...
wrote in message news:5643718@discussion.autodesk.com...
Standard operating procedure for me. I'd rather people learn how to find
stuff on their own, because somebody more knowledgeable is not always
available. In the process of looking for that information, more information
may be discovered that will also be useful. Also, I don't memorize the
entire VBA nomenclature so rather than trying to recall possibly faulty
information, or look it up and copy it (which anybody can do), I simply
provide a pointer to the required data.

Its also a good programming practice to get into. Instead of providing
multiple copies of data that may get unsynchronized, put the data in a
single place and provide references or pointers to it so any time its needed
the same information will result. Thats a little far-fetched metaphor, but
its late on a Friday...
0 Likes
Message 9 of 9

Anonymous
Not applicable
thank u all for solution.
u r right it is better to learn urself.many times i do the same.
i m new to vba and spent somuch time to learn arrays concept.as i m not doing it professionally it is not possible for me to spend much time.
i was frustrated. i tried my best.many try and error.and did not think it was so simple!
i appreciate ur help indeed.
thanx again.
0 Likes