WeakRef on Array of Objects

So how does one go about setting up an array of Objects to utilize WeakRefs? You can’t turn arrays into computed properties. I’m thinking the only way to do it is to set the array as type WeakRef. Then you need to create methods that would act as getter and setter. Kind of a pain since you really can’t refer to your object as a property…

Any other ideas?

Should the array be a weak reference or the elements in the array be weak references?

Two methods with the same name, the Sub using the Assigns keyword.

Why? It would be the same amount of lines of code as in the getter and setter of a computed property.

I thought it was about accessing an array of objects as property, not a sole object.

Operator_Subscript maybe?

From your post it is not clear to me what you want to achieve. Can’t you post what you have already?

Unless you’re doing something in the getter and setter, you could just leave the property in public scope… Creating the methods works the same as if you were using computed properties, except they don’t show the value in the debugger (but seeing as you are using WeakRefs, meh)

You could also wrap a class around the array which behaves like an array which will mostly work unless you need to be able to call something like Sort on it.

This doesn’t make any sense. The element of an array and the array itself have to be the same type. You can’t put an integer value into a string array for example.

[quote]
Two methods with the same name, the Sub using the Assigns keyword.[/quote]

That only works if your method code is in a module. I’m not wanting to create something global in scope here.

[quote]
Why? It would be the same amount of lines of code as in the getter and setter of a computed property.

I thought it was about accessing an array of objects as property, not a sole object.[/quote]

Yes, you then can never really access the property directly, but instead have to use a method to access the elements.

[quote]
Operator_Subscript maybe?[/quote]

I’m not sure what this has to do with anything.

[quote]
From your post it is not clear to me what you want to achieve. Can’t you post what you have already?[/quote]

So when you want to use WeakRefs, the best way to use them is via computed properties. Let’s say I have a property call TheObjects of type MyObject. I make it a computed property. Now I have a backing property called mTheObjects. I set that to type WeakRef.

Now in the Getter for TheObjects I have code such as:

If mTheObjects <> Nil and mTheObjects.Value <> Nil Then
   Return MyObject(mTheObjects.Value)
End If

And in the setter, I have:

mTheObjects = New WeakRef(value)

It’s translating that to work with arrays that will take some work I think Greg’s suggestion is the same type of idea I was thinking would be needed. Just a pain to do the work!

My motivation to do this was to make sure I didn’t have any references to these objects still hanging around when closing the app etc. I’ve got something somewhere that’s not being closed properly that’s causing my app to hang when I quit it. So my concern was that there were some references to objects that weren’t going away in some windows. But since these are arrays, I can wipe the values pretty easily be re-dimensioning the array to -1. So any objects that were held by the array are now released and if there’s no other references to them anywhere else, they go out of scope and go away. At least that’s how I think it happens!

[quote=348278:@Jon Ogden]Two methods with the same name, the Sub using the Assigns keyword.
That only works if your method code is in a module. I’m not wanting to create something global in scope here.[/quote]
Not correct. The Assigns keyword works for instance methods (and class methods) in classes too.

[code]Class MyClass

Private Property mArray() As WeakRef

Sub Constructor()
Redim mArray(-1)
End Sub

Function Count() As Integer
Return mArray.Ubound + 1
End Function

Sub Append(value As MyObject)
mArray.Append(New WeakRef(value))
End Sub

Sub Operator_Subscript(index As Integer, Assigns value As MyObject) // Setter method (using Assigns)
// test for valid index here
mArray(index) = New WeakRef(value)
End Sub

Function Operator_Subscript(index As Integer) As MyObject // Getter method
// test for valid index here
If mArray(index) <> Nil and mArray(index).Value <> Nil Then
Return MyObject(mArray(index).Value)
End If
End Function

// Remove and other necessary methods to be added

End Class[/code]

That only works if your method code is in a module. I’m not wanting to create something global in scope here.[/quote]
You’re thinking of Extends, not Assigns.