TheSwamp
Code Red => VB(A) => Topic started by: Mark on January 16, 2004, 02:45:51 PM
-
How come this doesn't work? What am I missing?
I'm trying to see if a selection set exists in the current dwg.
Sub ss_exists()
If ThisDrawing.SelectionSets.Item("SNEW") Then
MsgBox "selection set found: "
End If
End Sub
-
Item must be an integer.
Try this:
Sub ss_exists()
Dim I As Integer
For I = 1 To ThisDrawing.SelectionSets.Count
If ThisDrawing.SelectionSets.Item(I).Name = "SNEW" Then
MsgBox "selection set found: "
End If
Next
End Sub
-
Thanks Tim but, it didn't like that either!
fails on this line;
ThisDrawing.SelectionSets.Item(I).Name = "SNEW"
invalid argument index in item
-
How did you create the SS?
-
The problem is that while you can indeed filter for a selection set with
ThisDrawing.SelectionSets.Item("SNEW")
You cannot test for whether it actually exists or not from here alone. This is completely different than in lisp, where you can see if a variable has a value set to it, or if a function returned a non nil value.
VBA will not let you test against NULL in this instance.
Also if the selection set does not exist you will get a run time error "Key not found" and the application will crash.
-
Mark, using the code he posted, keep in mind that it can be affercted by the Option Base statement.
I'll bet you are using Option Base 0 (that is the default) so...
Sub ss_exists()
Dim I As Integer
For I = 0 To ThisDrawing.SelectionSets.Count -1
If ThisDrawing.SelectionSets.Item(I).Name = "SNEW" Then
MsgBox "selection set found: "
End If
Next
End Sub
-
How did you create the SS?
Set ss = ThisDrawing.SelectionSets.Add("SNEW")
-
>I'll bet you are using Option Base 0 (that is the default)
Beats me! But that did work, thanks.
-
Keith, would you mind writing a tutorial on Option Base and all its arguments?
-
Well dangit .... Lets load a bit more on me ... I still haven't finished my other thingy ... Let me see if I can put it together quick....
-
Ok, here goes ....
Option Base
Used at module level to declare the default lower bound for array subscripts.
Syntax
Option Base {0 | 1}
Remarks
Because the default base is 0, the Option Base statement is never required. If used, the statement must appear in a module before any procedures. Option Base can appear only once in a module and must precede array declarations that include dimensions.
Note The To clause in the Dim, Private, Public, ReDim, and Static statements provides a more flexible way to control the range of an array's subscripts. However, if you don't explicitly set the lower bound with a To clause, you can use Option Base to change the default lower bound to 1. The base of an array created with the Array function or the ParamArray keyword is zero; Option Base does not affect Array or ParamArray.
The Option Base statement only affects the lower bound of arrays in the module where the statement is located.
So, basically what that means is that unless you specify the base (meaning the numeric value) of the first subscript of an array, all arrays begin with item 0 not 1.
So
Option Base 1
Dim ThisArray(5)
creates
ThisArray(1) through ThisArray(5)
There is no ThisArray(0) in this example
But
Option Base 0
Dim ThisArray(5)
creates
ThisArray(0) through ThisArray(4)
There is no ThisArray(5)
So are you confused yet?
Don't sweat it...
Just make sure you always start array counting at 0 and end at the array length minus 1 and all will be fine.
Unless .......
There always has to be an exception now doesn't there....
So...
If an array is defined as:
Dim ThisArray(1 To 5)
The lower subscript (i.e the 1) will always be what was defined in the variable regardless of the setting of Option Base.
Also...
Note that the lower bound of an array created by Array or ParamArray is always 0 regardless of the setting of Option Base.
So Daron, how's that....
-
I remember now. That's great.
-
You are welcome :)