Detecting if an Array Changes

vendiddy

Well-known member
Joined
May 14, 2006
Messages
64
Programming Experience
1-3
In VB.NET, I would like to run a subroutine whenever any value in an array changes. Is there a way to do this? Thanks.

The only idea I have so far is creating a class that acts like an array.
 
Last edited:
Create a new class and inherit from whatever collection you want (ArrayList).

Then just override the functions you'd like, keep the actions from the base class, and then perform your subroutine.
 
Thanks for the advice. Here is have done:
VB.NET:
[SIZE=2][COLOR=#000000]Public Class VArray(Of ItemType)[/COLOR][/SIZE]
[SIZE=2][COLOR=#000000]Dim myArray(,) As ItemType[/COLOR][/SIZE]
[SIZE=2][COLOR=#000000]Public Sub New(ByVal XDim As Integer, ByVal YDim As Integer)[/COLOR][/SIZE]
[SIZE=2][COLOR=#000000]   ReDim myArray(XDim, YDim)[/COLOR][/SIZE]
[SIZE=2][COLOR=#000000]End Sub[/COLOR][/SIZE]
[SIZE=2][COLOR=#000000]Default Public Property Value(ByVal X As Integer, ByVal Y As Integer) As ItemType[/COLOR][/SIZE]
[SIZE=2][COLOR=#000000]   Get[/COLOR][/SIZE]
[SIZE=2][COLOR=#000000]       Return myArray(X, Y)[/COLOR][/SIZE]
[SIZE=2][COLOR=#000000]   End Get[/COLOR][/SIZE]
[SIZE=2][COLOR=#000000]   Set(ByVal value As ItemType)[/COLOR][/SIZE]
[SIZE=2][COLOR=#000000]       MessageBox.Show(String.Format("Element ({0}, {1}) changed from {2} to {3}", X, Y, myArray(X, Y), value))[/COLOR][/SIZE]
[SIZE=2][COLOR=#000000]       myArray(X, Y) = value[/COLOR][/SIZE]
[SIZE=2][COLOR=#000000]   End Set[/COLOR][/SIZE]
[SIZE=2][COLOR=#000000]End Property[/COLOR][/SIZE]
[SIZE=2][COLOR=#000000]End Class[/COLOR][/SIZE]

And then in the main form I can use this code:
VB.NET:
[SIZE=2][COLOR=#000000]     Dim arr As New VArray(Of Integer)(3, 5)[/COLOR][/SIZE]
[SIZE=2][COLOR=#000000]     arr(1, 1) = 12[/COLOR][/SIZE]
This works fine and acts sort of like a two-dimensional array. The question is, how do I make it act just like an array? If I had a real array, it would have the methods:
Clone
GetLength
GetLowerBound
GetUpperBound
Length
Rank
SetValue
And it wouldn't just be stuck to a 2D array like my class is.

Thanks.
 
vendiddy, here is an example inheriting List(Of String) and overloading (hiding by signature) those members that change the collection and raising an event for this:
VB.NET:
Public Class myList
    Inherits List(Of String)

    Public Event ListChanged()

    Public Overloads Sub Add(ByVal item As String)
        MyBase.Add(item)
        RaiseEvent ListChanged()
    End Sub

    Public Overloads Sub AddRange(ByVal collection As Collections.Generic.IEnumerable(Of String))
        MyBase.AddRange(collection)
        RaiseEvent ListChanged()
    End Sub

    'also overload Item, Clear, Inserts.., Removes.., Reverse?, Sort?
End Class
 
Thanks for the example. So I have to inherit from some type of collection (instead of an array)? The IDE gives me an error when I try say something like
VB.NET:
Public Class myList
    Inherits Array
    ...
End Class
If I can't inherit from an array, is there a way of making a collection syntactically act like an array? I want my class to be a replacement for an array with a few added features.

Thanks.
 
No, you have to use a collection. You could use a private array of your class and expose members to access it, but I don't see any point to do so.

By the way, the list collection have the ToArray method if you for some reason need to export the content as a plain array.
 
Okay. Thanks. Is there a way to make a 2-D collection? Would I have to do something like this?
VB.NET:
Dim myList as List (Of List(Of Integer))
 
That would make each item in the list its own List(Of Integer).

Another approach would be to make each item an array:
VB.NET:
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] myList2 [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] List([/SIZE][SIZE=2][COLOR=#0000ff]Of [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2]())[/SIZE]
I see what you did with itemType (a good thing!) in post 3, but when dealing with multidimensional arrays, considering the inherit fixed state and bad memory overhead of arrays, it would not be wise (and also can't be done, as I see it) to implement this dynamically/generic, instead implement the class for a specific number of dimensions. The other typical instance members of array you can expose through class members. The practical use of the class is often more important than the generic possibilities, it's impossible to make *everything* generic. I have to say, ever since I started programming I have never had any use of an array with more than three dimensions, mostly only one or two. (Perhaps its my experince that is too limited in this field..) Reflecting real world objects with OOP using collections of a business object type is replacing most of 'old days' array use.
 
Back
Top