VB Script, arrays and classes

Hi people

I'm running into a problem involving array reference.

In the following code, there is a simple class and one of its properties (namely "elements") is an array. Next, the class is instanciated and bound to the "var1" object variable. It should follow that "var1.elements" refers to the instance array.

Next, I try to assign a string value to the first 'cell' of the instance array :

var1.elements(0) = "hello"

and an anonymous array (through the Array(...) construct) is assigned to the second cell :

var1.elements(1) = Array("it's a beautiful day")

I can print out the values of var1.elements(0) and var1.elements(1)(0) without problems, but it's a different story attempting to modify the value of 'embedded' array :

var1.elements(1)(0) = "some other value"

... just doesn't work. It throws no errors, but the programmes behaves like var1.elements(1) returns a copy of the 'embedded' array and thus does not modify it. I mean that printing out var1.elements(1)(0) still displays "it's a beautiful day"

How come?

This behaviour doesn't occur using regular arrays instead of object variables :

dim var1(10)

var1(0) = Array("some value")

var1(0)(0) = "some other value"      >>> the 'embedded' array assignment is correctly taken into account.

class Test public elements() public sub Class_Initialize() redim elements(10) end sub end class REM ---------------------------------------------- dim var1 set var1 = new Test var1.elements(0) = "hello" var1.elements(1) = Array("it's a beautiful day") WScript.echo var1.elements(0), var1.elements(1)(0) REM >>> will display "hello it's a beautiful day"

var1.elements(0) = "yes it is" var1.elements(1)(0) = "and how are you?" WScript.echo var1.elements(0), var1.elements(1)(0)

REM >>> will display "yes it is it's a beautiful day"

Thanks in advance for your input.
  • Edited by Grobu 10 hours 22 minutes ago -
June 28th, 2015 4:43pm

Yes that is the behavior of arrays embedded in classes.  Use  Get/Set to adjust a class.  THe assigned array is only a reference and not a copy so it is not accessible.

Free Windows Admin Tool Kit Click here and download it now
June 28th, 2015 6:16pm

Thanks for your reply but I'm afraid I don't understand.

> THe assigned array is only a reference and not a copy so it is not accessible.

Are you saying that 

var1.elements(0) = Array(0,1)

will assign a reference to the anonymous array to elements(0), and because of that it is not accessible for manipulations outside of the class?

I have conducted the following tests about this class/array problem :

class Test

    public elements()
    
    public sub Class_Initialize()
        redim elements(0)
    end sub

    sub addArrayFromClass
        elements(0) = Array("M", "N")
    end sub

end class

REM ----------------------------------------------

dim var1, obj

set var1 = new Test

' test with string value - OK
var1.elements(0) = "test"
var1.elements(0) = "other"
WScript.echo var1.elements(0)                   ' >>> other


' test with object reference - OK
set obj = WScript.CreateObject("Scripting.Dictionary")
obj.item("greetings") = "hello"
set var1.elements(0) = obj
var1.elements(0).item("greetings") = "there"
WScript.echo var1.elements(0).item("greetings") ' >>> there
WScript.echo obj.item("greetings")              ' >>> there


' test with anonymous array - FAIL
var1.elements(0) = Array(0, 1)
var1.elements(0)(0) = 2
WScript.echo var1.elements(0)(0)                ' >>> 0

' test with copy of other array variable - FAIL
redim myArray(1)
myArray(0) = "A"
myArray(1) = "B"
var1.elements(0) = myArray
var1.elements(0)(0) = "Z"
WScript.echo var1.elements(0)(0)                ' >>> A
WScript.echo myArray(0)                         ' >>> A

' test with anonymous array assigned from inside class - FAIL
var1.AddArrayFromClass
WScript.echo var1.elements(0)(0)                ' >>> M
var1.elements(0)(0) = "L"
WScript.echo var1.elements(0)(0)                ' >>> M

So it seems that it is possible to manipulate the contents of a class array with strings, numbers, objects, but not embedded arrays. This is very annoying and unintuitive. What's the logic behind that?

June 28th, 2015 7:41pm

That is correct.  You cannot do that.  You can supply setter/getter methods and use specific "in-object" methods to manipulate arrays embedded in objects.

Free Windows Admin Tool Kit Click here and download it now
June 28th, 2015 10:32pm

You can replace the reference but not the elements:

class Test
    public elements(2)
end Class

set var1 = new Test
var1.elements(0) = "hello"
var1.elements(1) = Array("it's a beautiful day")

WScript.echo var1.elements(0), var1.elements(1)(0)

var1.elements(0) = "yes it is"
var1.elements(1) = Array("and how are you?")

WScript.echo var1.elements(0) ,var1.elements(1)(0)
June 28th, 2015 10:34pm

Consider the following:

class Test
    public elements(2)
end Class

set var1 = new Test

var1.elements(0) = "hello"
var1.elements(1) = Array("it's a beautiful day")

WScript.echo var1.elements(0), var1.elements(1)(0)

var1.elements(0) = "yes it is"
a2=Array("and how are you?")
var1.elements(1) = a2

WScript.echo var1.elements(0) ,var1.elements(1)(0)

a2(0) = "XXXXXXXXXXXXXXXX"
WScript.Echo "A2  ===  " & a2(0)
var1.elements(1) = a2
WScript.Echo  "ELEMENTS ====== " & var1.elements(1)(0)

var1.elements(1)(0) = "YYYYYYYYYY"
WScript.Echo "ELEMENTS 1 ===== " & var1.elements(1)(0)
WScript.Echo "A2  =====" & a2(0)

Free Windows Admin Tool Kit Click here and download it now
June 28th, 2015 10:43pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics