|
-
May 27th, 2008, 11:01 AM
#1
Thread Starter
Addicted Member
Undo feature - stack, collection of collections or what?
Hi,
I am writing a simple drawing application where the user places a series of data points on the screen.
Each of the data points has a number of properties as defined by :
Code:
<Serializable()> _
Public Class objDataPoint
Public Location As Point
Public Moveable As Boolean
Public Time As Single
Public Power As Single
Public Selected As Boolean
Public Sub New(ByVal Location As Point, ByVal Moveable As Boolean, ByVal Time As Single, ByVal Power As Single, ByVal Selected As Boolean)
'Set the attributes
Me.Location = Location
Me.Moveable = Moveable
Me.Time = Time
Me.Power = Power
Me.Selected = Selected
End Sub
End Class
The whole drawing is defined by a collection of data points so I have :
Code:
Private colDataPoints As New Collection
and as the user adds a data point to the drawing canvas I just add another member to the collection.
Now I want to implement an Undo function and I am not sure how to do this.
My first idea was to create another collection and have a collection of collections but I could not get this to work.
I have done a lot of searching and come up with a number of suggestions including serialization and using a Stack object.
This is my attempt at using the Stack object but it doesn't work either.
Code:
Private HistoryStack As New Stack
Private Sub SaveUndoInformation()
HistoryStack.Push(colDataPoints)
tsbUndo.Enabled = True
End Sub
Private Sub tsbUndo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbUndo.Click
'Remove all current data points.
colDataPoints.Clear()
'Make sure that no point is currently selected.
m_intSelectedDataPoint = -1
'Extract the data.
Dim colTemp As New Collection
colTemp = HistoryStack.Pop
colDataPoints = HistoryStack.Pop
If HistoryStack.Count = 0 Then tsbUndo.Enabled = False
'Force the display area to redraw after all new data points have been loaded.
picCanvas.Invalidate()
End Sub
I have been scratching my head on this all day so any help is appreciated.
Using Visual Studio 2008.
I am not young enough to know everything - Oscar Wilde
-
May 27th, 2008, 11:14 AM
#2
Re: Undo feature - stack, collection of collections or what?
First off, i wouldnt use a 'Collection' class for the objDataPoints, i would use a List(Of T).
For the undo feature I would create a separate Stack, and upon undoing, i would remove the last element in the objDataPoints List and push it onto the stack. That way you could also have a 'redo' feature, where you would simply pop the first element of the stack again and back into the List(Of T).
-
May 27th, 2008, 11:22 AM
#3
Thread Starter
Addicted Member
Re: Undo feature - stack, collection of collections or what?
Thanks for the quick reply!
So I can learn from my mistakes. Why is using a Collection not a good way to go go and why is List(Of T) better?
I was trying to stop some of the complexities of this system from confusing the question but, because of the way the system works, sometimes when the user adds a single point, the program will automatically add more than one point.
Therefore, I really do need to save snapshots of the entire drawing (i.e. all of the data points).
Using Visual Studio 2008.
I am not young enough to know everything - Oscar Wilde
-
May 27th, 2008, 11:27 AM
#4
Re: Undo feature - stack, collection of collections or what?
Collection is a leftover from VB6, and it is not strongly-typed. The List(Of T) is strongly-typed and performs better.
-
May 27th, 2008, 11:32 AM
#5
Thread Starter
Addicted Member
Re: Undo feature - stack, collection of collections or what?
Thanks for the explanation.
I have migrated from VB6 to .NET so am still trying to shake off some of the old ways of doing things and learning the new ways.
I will change from Collection to List(Of T).
How about the second part of my reply.
Won't I have the same problem saving and retreiving the entire List(Of T) as I am having when using the Collection?
Using Visual Studio 2008.
I am not young enough to know everything - Oscar Wilde
-
May 27th, 2008, 11:34 AM
#6
Re: Undo feature - stack, collection of collections or what?
Do you mean that you want to be able to save the entire List of objDataPoints to a file, and load it back again? You could definitely use serialization for that. If thats what you need I'll give you an example.
-
May 27th, 2008, 07:15 PM
#7
Re: Undo feature - stack, collection of collections or what?
A Stack is the right option for an Undo feature. That said, you should use a generic stack rather than a standard Stack. i.e. use a Stack(Of T) just as you're using a List(Of T). You could also use a second Stack to provide a Redo feature, but I'd get the first bit right first.
-
May 28th, 2008, 03:21 AM
#8
Thread Starter
Addicted Member
Re: Undo feature - stack, collection of collections or what?
Thanks for the replies,
Atheist,
Yes, because more than one data point can be changed with each user action, I want to save the entire list of data points.
I am already using Serialization in a file Save/Load feature but was not sure if this was the right was to go with the Undo feature. Would I be saving copies to the hard disk or is there some way to serialize and save to a Stack?
jmcilhinney,
All of this is just showing me that I have a lot more to learn about .NET. Atheist suggested List(Of T) rather than the Collection that I am currently using. First thing that I have to do is change my implementation to use that method. Then I will look at using Stack(Of T) for the Undo.
Using Visual Studio 2008.
I am not young enough to know everything - Oscar Wilde
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|