Storing arrays in the application object

T

Terje

Hello Group. I have problems storing arrays in the application object.

First I create a string array and put it in an application variable:
Application("MyArray") = Split(RedFile(path), vbCrLf)

Then I create a class where one property is pointing to the variable above:
Class MyClass
Public Property Get MyArray(): MyArray= Application("MyArray"): End Property
End Class

Then I create an instance of the class:
Dim A: Set A = New MyClass

This way I can access all my application variables with the handy
A.MyArray, it's handy because it is short.

I am able to read UBound(A.MyArray) and typename(A.MyArray) returns
Variant(). But I can't access specific indexes like A.MyArray(0). This
throws error 450
Wrong number of arguments or invalid property assignment.

However, I found that when I copy the array into another variable aCopy
= A.MyArray, then I can access this array as I would expect. I read
somewhere on the Internet that the application object is implemented as
a collection, but why then can I read the UBound property? And why is
Variant() returned as typename?

terje
 
A

Anthony Jones

Terje said:
Hello Group. I have problems storing arrays in the application object.

First I create a string array and put it in an application variable:
Application("MyArray") = Split(RedFile(path), vbCrLf)

Then I create a class where one property is pointing to the variable
above:
Class MyClass
Public Property Get MyArray(): MyArray= Application("MyArray"): End
Property
End Class

Then I create an instance of the class:
Dim A: Set A = New MyClass

This way I can access all my application variables with the handy
A.MyArray, it's handy because it is short.

I am able to read UBound(A.MyArray) and typename(A.MyArray) returns
Variant(). But I can't access specific indexes like A.MyArray(0). This
throws error 450
Wrong number of arguments or invalid property assignment.

You've created a property called MyArray hence MyArray(0) is expecting to
call a property that takes a parameter. For example:-

Public Property Get MyArray(ByVal index)
MyArray = Application("MyArray")(index)
End Public

This would let you access an element in the array using A.MyArray(0). Of
course you then can't just retrieve the array as a whole.
However, I found that when I copy the array into another variable aCopy =
A.MyArray, then I can access this array as I would expect.

Yes by placing the copy of an array into a variable and using aCopy(0) you
are no longer attempting to call a property. VBscript inteprets ()
following variable as an array index opertaion. Whereas () following a
property or method name is interperted as a procedure call passing any thing
inside the () as parameters.

I read somewhere on the Internet that the application object is implemented
as a collection, but why then can I read the UBound property?

The application object is, it has no UBound property, you are talking about
the array value retrieved from the application object.
And why is Variant() returned as typename?

Because the property returns an array of variants.


All that said you need to be careful with arrays in the application object.
If you really need to use one then you should be aware that accessing it
creates a copy. Hence your class should copy it into a local variable and
then access values from the local copy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top