This article talks about storing the snapshot of data of current state in an array object. This can be applied to an ArrayList, Queue, Stack object, or any other datatype that implements the ICollection interface. This is done by using the CopyTo method declared in the ICollection interface. The following example, method TakeSnapshotOfList, accepts any type that implements the ICollection interface and takes a snapshot of the entire’s contents. This snapshot is returned as an object array.
public static object[] TakeSnapshotOfList(ICollection theList) { object[] snapshot = new object[theList.Count]; theList.CopyTo(snapshot, 0); return (snapshot); }
The GetKeys method uses the Keys property. Once the ICollection of keys is returned through this property, a new ArrayList is created to hold the keys. This ArrayList is then returned to the caller. The GetValues method works in a similar manner except that it uses the Values property.
The following method creates a Queue object, enqueues some data, and then takes a snapshot of it:
public static void TestListSnapshot() { Queue someQueue = new Queue(); someQueue.Enqueue(1); someQueue.Enqueue(2); someQueue.Enqueue(3); object[] queueSnapshot = TakeSnapshotOfList(someQueue); }
The TakeSnapshotOfList is useful when you want to record the state of an object that implements the ICollection interface. This “snapshot” can be compared to the original list later on to determine what, if anything, has changed in the list. Multiple snapshots can be taken at various points in an applications run to show the state of the list or lists over time.
The TakeSnapshotOfList method could easily be used as a logging/debugging tool for developers. Take, for example, an ArrayList that is being corrupted at some point in the application. You can take snapshots of the ArrayList at various points in the application using the TakeSnapshotOfList method and then compare the snapshots to narrow down the list of possible places where the ArrayList is being corrupted.
Popularity: 2% [?]
RSS feed for comments on this post · TrackBack URI
Leave a reply