View Full Version : creating groups

2004-12-01, 04:27 PM
How is it possible to create an AutoCAD group in VBA and add things to it without knowing how many objects will be in it? Do you create a selection set first and append the group with that? Unfortunately the number of items drawn on the screen that need to be added to a group is random and determined during runtime. I tried to set the number of objects once created to a variable and then pass the variable to an array
Dim Plgroupitems(0 To Variable) As Object but an error occurs stating that a "Constant" is required here. Any thoughts on this?

Ed Jobe
2004-12-01, 05:09 PM
You can't dim an array with a variable. Look in help for the Redim statement for examples. Basically, you have to first declare the array without any dimensions, but followed by opening and closing parens to indicate that it will later be dimensioned. Then you redim later in your code, with, e.g. the count of a selectionset.

Also, you might want to use AcadEntity to represent any onscreen ent, rather than the excessively generic Ojbect.

2004-12-03, 02:50 PM
If I had a variable in the program, say Xcount = 5, is there a way to get this variable to be seen as a constant where I can Redim the array (0 to Xcount) or will I have to load that number somewhere in a control so it sees it as a constant? Unfortunately, the Xcount variable is obtained through a series of calculations where a different number of entities will be created and I want to add these to a group.

The only work around I can figure would be to have a bogus text label not visible on the form where I change the color or something to the Xcount variable (Label1.Backcolor = Xcount) and then Redim the array (0 to Label1.Backcolor).

2004-12-03, 03:18 PM
You can use a variable to redim an array, what Ed was saying was that you have to have a constant when you dim it or leave it blank. Take a look at these examples:

Dim myVar(5) 'legal
Dim i As Integer
i = 10
Dim myVar(i) 'illegal
ReDim myVar(i) 'legal

This is from the help:

ReDim Statement
Used at procedure level (javascript:hhobj_4.Click()) to reallocate storage space for dynamic array variables (javascript:hhobj_5.Click()).


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

The ReDim statement syntax has these parts:

PartDescriptionPreserveOptional. Keyword (javascript:hhobj_6.Click()) used to preserve the data in an existing array (javascript:hhobj_7.Click()) when you change the size of the last dimension.varnameRequired. Name of the variable; follows standard variable naming conventions.subscriptsRequired. Dimensions of an array variable; up to 60 multiple dimensions may be declared. The subscripts argument (javascript:hhobj_8.Click()) 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.

typeOptional. Data type (javascript:hhobj_9.Click()) of the variable; may be Byte (javascript:hhobj_10.Click()), Boolean (javascript:hhobj_11.Click()), Integer (javascript:hhobj_12.Click()), Long (javascript:hhobj_13.Click()), Currency (javascript:hhobj_14.Click()), Single (javascript:hhobj_15.Click()), Double (javascript:hhobj_16.Click()), Decimal (javascript:hhobj_17.Click()) (not currently supported), Date (javascript:hhobj_18.Click()), String (javascript:hhobj_19.Click()) (for variable-length strings), String * length (for fixed-length strings), Object (javascript:hhobj_20.Click()), Variant (javascript:hhobj_21.Click()), a user-defined type (javascript:hhobj_22.Click()), or an object type (javascript:hhobj_23.Click()). 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.


The ReDim statement (javascript:hhobj_24.Click()) 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 (javascript:hhobj_25.Click()). 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 (javascript:hhobj_26.Click()) 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 (javascript:hhobj_27.Click()) or procedure level (javascript:hhobj_28.Click()). If another variable with the same name is created later, even in a wider scope (javascript:hhobj_29.Click()), 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.