Author Topic: If function help  (Read 4497 times)

0 Members and 1 Guest are viewing this topic.

Mark

  • Custom Title
  • Seagull
  • Posts: 28685
If function help
« 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
TheSwamp.org  (serving the CAD community since 2003)

TR

  • Guest
If function help
« Reply #1 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

Mark

  • Custom Title
  • Seagull
  • Posts: 28685
If function help
« Reply #2 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
TheSwamp.org  (serving the CAD community since 2003)

John Kaul (Se7en)

  • Administrator
  • Needs a day job
  • Posts: 9268
If function help
« Reply #3 on: January 16, 2004, 03:29:49 PM »
How did you create the SS?
“Common sense is not so common.” ~Voltaire

--> Donate to TheSwamp.org <--

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16721
  • Superior Stupidity at its best
If function help
« Reply #4 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.
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16721
  • Superior Stupidity at its best
If function help
« Reply #5 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
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Mark

  • Custom Title
  • Seagull
  • Posts: 28685
If function help
« Reply #6 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")
TheSwamp.org  (serving the CAD community since 2003)

Mark

  • Custom Title
  • Seagull
  • Posts: 28685
If function help
« Reply #7 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.
TheSwamp.org  (serving the CAD community since 2003)

daron

  • Guest
If function help
« Reply #8 on: January 16, 2004, 03:58:32 PM »
Keith, would you mind writing a tutorial on Option Base and all its arguments?

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16721
  • Superior Stupidity at its best
If function help
« Reply #9 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....
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16721
  • Superior Stupidity at its best
If function help
« Reply #10 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....
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

daron

  • Guest
If function help
« Reply #11 on: January 16, 2004, 04:40:37 PM »
I remember now. That's great.

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16721
  • Superior Stupidity at its best
If function help
« Reply #12 on: January 16, 2004, 10:18:34 PM »
You are welcome :)
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal