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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to delete elements from array with stack count zero???? 1

Status
Not open for further replies.

WomanPro

Programmer
Nov 1, 2012
180
0
0
GR
Hello everybody.
Again I have my structure
Public Structure GatesPositions
Public pos As Integer
Public stack As Stack
End Structure

and an array of the defined type
Public UsrCheckerGates() As GatesPositions

How can I delete the element or elements of the array that the stack count is zero????
It is an array and I don't know how can I delete a random item. I wish there was a method, but nothing like that. I thought to copy elements with non zero count stack to another array and delete all elements from UsrCheckerGates and recopy them to the UsrCheckerGates() array again, but I wish I could do that in a simplest way. Any suggestions please???
Any help will be much appreciated.

Thank you very much
in advanced.
 
I suggest using a Collection instead of an array. Collections have the RemoveAt method, which will remove an element form the collection at the specified index, and will adjust the remaining elements after the removed element to fill the "gap" left by the removal.

Public UsrCheckerGates As Collection(Of GatesPositions)

UsrCheckerGates = New Collection(Of GatesPositions)


To add a GatesPositions object to the collection, use the Add method:

Dim gp As New GatesPositions

UsrCheckerGates.Add(GatesPositions)

To loop through and remove GatesPositions objects with an empty stack, do this:

For i As Integer = UsrCheckerGates.Count - 1 to 0 Step -1 'loop through collection backwards
If UsrCheckerGates(i).stack.Count = 0 then
UsrCheckerGates.RemoveAt(i)
EndIf
Next



I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Thank you Jebenson, but that's wrong way :(

Public UsrCheckerGates As Collection(Of GatesPositions)

gives me error
'Microsoft.VisualBasic.Collection' has no type parameters and so cannot have type arguments.
 
Replace [tt]Collection[/tt] with [tt]List[/tt]. That should do what you need.
 
I forgot to say, that [tt]List[/tt] also has a [tt]RemoveAt[/tt] method, sorry.
 
Is it possible to change values of elements to the list?????
For example to say UsrCheckerGates(indx).pos = 15 because I am getting error Expression is a value and therefore cannot be the target of an assignment. Is there a way to do that??? If yes, then to use list instead would be a good solution, if no, that's impossible
 
This seems to do what you need:

Code:
Public Class Form1

	Public Structure GatesPositions
		Public pos As Integer
		Public stack As Stack
	End Structure

	Public UsrCheckerGates As List(Of GatesPositions)


	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

		UsrCheckerGates = New List(Of GatesPositions)

	End Sub

	Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

		Dim gp As New GatesPositions
		gp.pos = 1
		gp.stack = New Stack
		UsrCheckerGates.Add(gp)

	End Sub

	Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

		Dim gp As New GatesPositions
		gp = UsrCheckerGates(0)
		gp.pos = 5
		gp.stack = UsrCheckerGates(0).stack
		UsrCheckerGates(0) = gp

	End Sub

	Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click

		MessageBox.Show(UsrCheckerGates(0).pos.ToString)

	End Sub
End Class

To test it click button1, then button2, then BUTTON 4, then button3, then BUTTON4 and you will see the pos value change from 1 to 5.
 
UsrCheckerGates() is an array created by me, that during the program is running, could change values of elements stacks and pos at any time. For example when a checker has to be moved, it will be deleted from the stack that already is and will be added to another stack that already exist either a new one and it's pos (position) will automatically change to the new one position. That's why the user can move all the checkers from a stack to another positions, so that other stack/stacks will be created and the stack that sever will have count elements zero. It can happen to many stacks and I would like to delete the elements of the UsrCheckerGates() that have zero stack count. Hope it helps you understand somehow.
 
I've added a Button5 to the above to hopefully enable you to see how you can manipulate the parts of and the items in the List.

Code:
	Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click

		Dim gp As New GatesPositions
		gp.pos = 25
		gp.stack = New Stack
		gp.stack.Push("This is on the stack!!!")
		MessageBox.Show("Pos = " & gp.pos.ToString & " : " & gp.stack.Count.ToString & " item(s) on the stack")
		MessageBox.Show(gp.stack.Pop.ToString & " and has now been popped off the stack")
		MessageBox.Show("There are now " & gp.stack.Count.ToString & " items on the stack")

		'Then you can move gp items freely between each other or edit or delete them as appropriate
		'Hopefully you can now see how you can easily manipulate the items in the List

	End Sub
 
By the way, I'm using a List in my example above, not an Array.
 
Thank you softhemc
finally created that subroutine that copies all the elements of UsrCheckerGates that stack count is not zero and then recopy them to the UsrCheckerGates. It works fine at the moment. I will try to change all the program to use the list, I hope it will have an effect if no, it can help me in that way too!!! Thank you so much!!!

Public Sub DeleteZeroCountStacks()
Dim gp As List(Of GatesPositions)

gp = New List(Of GatesPositions)

Dim i As Byte
Dim CntStacks As Byte = 1
For i = 0 To UsrCheckerGates.Length - 1
If UsrCheckerGates(i).stack.Count <> 0 Then
gp.Add(UsrCheckerGates(i))
End If
Next
Dim Cnt As Byte
Cnt = gp.Count
ReDim UsrCheckerGates(Cnt - 1)

For i = 0 To Cnt - 1
UsrCheckerGates(i).stack = New Stack
UsrCheckerGates(i).stack = gp(i).stack
UsrCheckerGates(i).pos = gp(i).pos
Next
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top