Author Topic: Select sets variables [reference type]- an FYI  (Read 1863 times)

0 Members and 1 Guest are viewing this topic.

T.Willey

  • Needs a day job
  • Posts: 5251
Select sets variables [reference type]- an FYI
« on: March 09, 2007, 03:19:39 PM »
Selection set variables are set to the same object in memory.  Maybe some code might explain it better.  I just learned this today so I thought I would share.
Code: [Select]
(
 (lambda (/ ss ss1)
  (setq ss (ssget))
  (setq ss1 ss)
  (print (sslength ss))
  (print (sslength ss1))
  (ssdel (ssname ss 0) ss)
  (print (sslength ss))
  (print (sslength ss1))
  (princ)
 )
)
Quote
19
19
18
18

I didn't think this would happen, but after doing some coding in C# it seems that the variables 'ss' and 'ss1' are reference variables.  The both reference the same block of memory.

If anything I have stated is wrong, please let me know.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Select sets variables [reference type]- an FYI
« Reply #1 on: March 09, 2007, 03:47:16 PM »
True story and good observation Tim. It's also why when you pass a selection set to a function you have to be careful (because it's treated as a reference).

Brief illumination:

Code: [Select]
(   (lambda ( / foo ss )

        (defun foo ( ss )
            (if (and
                    (eq 'pickset (type ss))
                    (< 0 (sslength ss))
                )
                (ssdel (ssname ss 0) ss)
            )
        )

        (if (setq ss (ssget "x"))

            (progn

                (princ
                    (strcat
                        "Selection set ss's length before (foo ss): "
                        (itoa (sslength ss))
                        "\n"
                    )   
                )

                (foo ss)

                (princ
                    (strcat
                        "Selection set ss's length after (foo ss): "
                        (itoa (sslength ss))
                        "\n"
                    )   
                )
            )
           
            (princ "Errrm, try this in a dwg with some entities.")
           
        )

        (princ)

    )
)

Not suggesting you don't know this Tim; it's for the benefit of those that might not.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Select sets variables [reference type]- an FYI
« Reply #2 on: March 09, 2007, 03:52:57 PM »
True story and good observation Tim. It's also why when you pass a selection set to a function you have to be careful (because it's treated as a reference).

Brief illumination:

Code: [Select]
<snip>
Not suggesting you don't know this Tim; it's for the benefit of those that might not.
Thanks for that Michael.  I didn't even think of that, but it makes good sense when I do.  :-D

Glad to see you posting again, hope all is/was well and good with you.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.