Author Topic: A safearray is a kind of reference?  (Read 1243 times)

0 Members and 1 Guest are viewing this topic.

highflyingbird

  • Bull Frog
  • Posts: 415
  • Later equals never.
A safearray is a kind of reference?
« 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.  
« Last Edit: June 27, 2012, 09:08:26 AM by HighflyingBird »
I am a bilingualist,Chinese and Chinglish.

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: A safearray is a kind of reference?
« Reply #1 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).
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.