Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to copy a stack to another without any affections when I pop items?

Status
Not open for further replies.

WomanPro

Programmer
Nov 1, 2012
180
GR
Hello everyone
I have a stack of buttons creating at runtime and I would like to copy all it's elements to another stack, before I pop items of the initial stack without affecting the ResultStack.

Dim InitialStack, CopiedStack As Stack(Of Button)
InitialStack = New Stack(Of Button)
InitialStack.Push(btn1)
InitialStack.Push(btn2)
InitialStack.Push(btn3)
'btn1, btn2, btn3 are examples, in reallity the stack takes elements during runtime.

CopiedStack = New Stack(Of Button)
CopiedStack = InitialStack
InitialStack.Pop()
if I say that btn3 is poped of both stacks too.
The what I want to do is to pop the peek item of InitialStack, without affect CopiedStack
Any ideas please? Any help will be much appreciated.

Thank you so much
in advanced.
 
As your code stands at the moment, both stacks refer to the same item since you are setting copiedstack to the same instance as initialstack.

To achieve what you want you will need to copy the elements of one stack to another. If this is something that you will need to do often then create a function that copies from one to another passing the source stack as a parameter. Don't forget that the function should be of type stack. Then you could call something like CopiedStack = CopyTheStack(InitialStack). The declaration of CopyTheStack would need to be something like: Private Function CopyTheStack(StackToCopy As Stack) As Stack.
 
Thank you dear!!!!
I understand what you say, but my problem is how can I copy a stack to another so that when I pop items from the InitialStacke not to be popped from the CopiedStack? Is it possible to do that kind of copy and how???
However I just tried the following and it worked. I know that I copy elements from a stack to an array but I don't know as I just mentioned how could I do the what I want to achieve. Do you have any suggestions???

Dim Myarray As Array
Myarray = InitialStack.ToArray()
InitialStack.Pop()
For i As Byte = 0 To Myarray .Length - 1
CopiedStack.Push(Myarray (i))
Next
 
Ok, I've coded you an example.

Create a test app as follows:

On the form -
2 Buttons (CopyToStack2 and PopFromStack1)
1 ListBox (SourceLB with: [tt]SourceLB.Items.AddRange(New Object() {"2", "4", "6", "8", "1", "3", "5", "7", "9"})[/tt]
3 TextBoxes (Stack1TB, Stack2TB, PoppedFromStack1TB)
Add any appropriate labels.

To use:
DoubleClick values in SourceLB and they immediately get pushed to Stack1 and the contents of Stack1 are immediately shown in Stack1TB
Click PopFromStack1 and the top value in Stack1 is removed from Stack1 and Stack1TB and is placed in PoppedFromStack1TB (for your info)
At any time click CopyToStack2. Stack2TB is an exact copy of the current state of Stack1 and is duplicated in Stack2TB. Further popping from (or for that matter pushing to) Stack1 will not be reflected in Stack2 until you reissue the CopyFromStack1 command.

Code:
Public Class Form1

	Private Stack1 As New Stack(Of Integer)
	Private Stack2 As New Stack(Of Integer)

	Private Sub RefillTextBox(tb As TextBox, source As Stack(Of Integer))

		tb.Text = ""
		For a As Integer = 0 To source.Count - 1
			tb.Text &= source.ElementAt(a).ToString & Environment.NewLine
		Next

	End Sub

	Private Sub CopyStack(source As Stack(Of Integer), destination As Stack(Of Integer))

		destination.Clear()
		For a As Integer = source.Count - 1 To 0 Step -1
			destination.Push(source.ElementAt(a))
		Next

	End Sub


	Private Sub SourceLB_MouseDoubleClick(sender As Object, e As MouseEventArgs) Handles SourceLB.MouseDoubleClick

		Dim x As Integer = CInt(SourceLB.SelectedItem)
		Stack1.Push(x)
		RefillTextBox(Stack1TB, Stack1)

	End Sub

	Private Sub CopyToStack2_Click(sender As Object, e As EventArgs) Handles CopyToStack2.Click

		CopyStack(Stack1, Stack2)
		RefillTextBox(Stack2TB, Stack2)

	End Sub

	Private Sub PopFromStack1_Click(sender As Object, e As EventArgs) Handles PopFromStack1.Click

		Dim x As Integer = Stack1.Pop
		RefillTextBox(Stack1TB, Stack1)
		RefillTextBox(Stack2TB, Stack2)
		PoppedFromStack1TB.Text &= x.ToString & Environment.NewLine

	End Sub
End Class
 
Error at
tb.Text &= source.ElementAt(a).ToString & Environment.NewLine
destination.Push(source.ElementAt(a))

Error 1 'ElementAt' is not a member of 'System.Collections.Generic.Stack(Of Integer)
What is source?
 
I'm using Visual Studio 2012 Ultimate Edition. If you are using an earlier version, ElementAt may not be available. ElementAt requires .Net Framework 3.5 minimum.

The other options you can look at are the CopyTo method or the GetEnumerator method. Help has examples of using both of these. Unfortunately I don't have enough time at the moment to create an example for you. (May be later in the day).
 
Thank you softhemc. It's true I am using VB.net 2005, I have .net Framework 3.5 sp1 but I get that error. This method isn't provided.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top