TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: highflyingbird on June 27, 2012, 09:03:08 AM

Title: A safearray is a kind of reference?
Post by: highflyingbird on June 27, 2012, 09:03:08 AM
A safearray is a kind of  reference?
It means ,when we copy a safearray , if  the element in origin has been changed,the copy one changed also.
Let's see this test:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:test (/ lst oArray rArray value1 value2 List->SafeArray)
  2.   (defun List->SafeArray (L DataType)
  3.       (vlax-make-safearray DataType
  4.         (cons 0 (1- (length L)))
  5.       )
  6.       L
  7.     )
  8.   )
  9.   (setq lst '(1 2 3 4 5 6))
  10.   (setq oArray (list->safearray lst vlax-vbInteger))    ;Make a new safearray
  11.   (setq rArray oArray)                                  ;->just a kind of reference
  12.  
  13.   (vlax-safearray-put-element oArray 0 7)               ;Change the first element to a new value
  14.  
  15.   (setq value1 (vlax-safearray-get-element oArray 0))   ;Get the 1st element value from origin
  16.   (setq value2 (vlax-safearray-get-element rArray 0))   ;The copy one has been changed at the same time!!!
  17.   (= value1 value2)                                     ;they are equal. --> T
  18.  
  19.   (vlax-safearray-put-element rArray 5 8)               ;do another test
  20.  
  21.   (setq value1 (vlax-safearray-get-element rArray 5))   ;Get the 1st element value from the copy
  22.   (setq value2 (vlax-safearray-get-element oArray 5))   ;The origin has been changed at the same time!!!
  23.   (= value1 value2)                                     ;they are equal.  --> T
  24.  
  25.   (princ)
  26. )
  27.  
Title: Re: A safearray is a kind of reference?
Post by: irneb on June 27, 2012, 09:43:02 AM
It does seem so yes. I'm guessing it's implemented along the same lines as a selection set (e.g. using ssadd / ssdel modifies the original).