TheSwamp

Code Red => VB(A) => Topic started by: Mark on January 16, 2004, 02:45:51 PM

Title: If function help
Post 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.
Code: [Select]

Sub ss_exists()
    If ThisDrawing.SelectionSets.Item("SNEW") Then
        MsgBox "selection set found: "
    End If
End Sub
Title: If function help
Post by: TR on January 16, 2004, 03:07:09 PM
Item must be an integer.

Try this:

Code: [Select]

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
Title: If function help
Post by: Mark on January 16, 2004, 03:21:22 PM
Thanks Tim but, it didn't like that either!
fails on this line;
ThisDrawing.SelectionSets.Item(I).Name = "SNEW"

invalid argument index in item
Title: If function help
Post by: John Kaul (Se7en) on January 16, 2004, 03:29:49 PM
How did you create the SS?
Title: If function help
Post by: Keith™ on January 16, 2004, 03:36:07 PM
The problem is that while you can indeed filter for a selection set with
Code: [Select]

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.
Title: If function help
Post by: Keith™ on January 16, 2004, 03:42:16 PM
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...

Code: [Select]


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
Title: If function help
Post by: Mark on January 16, 2004, 03:43:52 PM
Quote from: Se7en
How did you create the SS?

Code: [Select]
Set ss = ThisDrawing.SelectionSets.Add("SNEW")
Title: If function help
Post by: Mark on January 16, 2004, 03:53:31 PM
>I'll bet you are using Option Base 0 (that is the default)
Beats me! But that did work, thanks.
Title: If function help
Post by: daron on January 16, 2004, 03:58:32 PM
Keith, would you mind writing a tutorial on Option Base and all its arguments?
Title: If function help
Post by: Keith™ on January 16, 2004, 04:03:42 PM
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....
Title: If function help
Post by: Keith™ on January 16, 2004, 04:21:30 PM
Ok, here goes ....

Option Base
Quote from: Microsoft

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....
Title: If function help
Post by: daron on January 16, 2004, 04:40:37 PM
I remember now. That's great.
Title: If function help
Post by: Keith™ on January 16, 2004, 10:18:34 PM
You are welcome :)