none
How to stop a loop when stack is empty RRS feed

  • Question

  • I am trying to empty the variables from my stack one by one using a loop which I want to end when the stack is empty.  I can empty the variables easily but do not know how to code the empty stack part of the loop. Does anyone have any examples I can look at, please?
    • Edited by CrazyMum123 Friday, February 2, 2018 11:00 AM
    Friday, February 2, 2018 10:59 AM

Answers

  • Do you mean something like this

    Dim myStack As New Stack()
    myStack.Push("The")
    myStack.Push("quick")
    myStack.Push("brown")
    myStack.Push("fox")
    
    While myStack.Count <> 0
        myStack.Pop()
    End While
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by CrazyMum123 Friday, February 2, 2018 11:40 AM
    Friday, February 2, 2018 11:19 AM
    Moderator
  • If I understand correctly control the loop using Stack.Count.  Each iteration of the loop that removes an element from the stack will reduce the count.  Presumably you are doing something with the element that has been popped off the stack.  Terminate the loop when Stack.Count has reached 0.
    • Marked as answer by CrazyMum123 Friday, February 2, 2018 11:40 AM
    Friday, February 2, 2018 11:21 AM

All replies

  • And the sense to loop through it one by one instead of Stack.Clear ?

    https://msdn.microsoft.com/en-us/library/system.collections.stack.clear(v=vs.110).aspx

    There is an old Dutch joke about an old lady calling the police.

    1. The old woman "Policeman the two persons at the other side outside the window do dirty things".
    2. The policeman: "I see nothing".
    3. The old woman: "No but when you take this ladder and go to the top you can see it"

    Success Cor

    Friday, February 2, 2018 11:09 AM
  • Do you mean something like this

    Dim myStack As New Stack()
    myStack.Push("The")
    myStack.Push("quick")
    myStack.Push("brown")
    myStack.Push("fox")
    
    While myStack.Count <> 0
        myStack.Pop()
    End While
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by CrazyMum123 Friday, February 2, 2018 11:40 AM
    Friday, February 2, 2018 11:19 AM
    Moderator
  • If I understand correctly control the loop using Stack.Count.  Each iteration of the loop that removes an element from the stack will reduce the count.  Presumably you are doing something with the element that has been popped off the stack.  Terminate the loop when Stack.Count has reached 0.
    • Marked as answer by CrazyMum123 Friday, February 2, 2018 11:40 AM
    Friday, February 2, 2018 11:21 AM
  • Do you mean something like this

    Dim myStack As New Stack()
    myStack.Push("The")
    myStack.Push("quick")
    myStack.Push("brown")
    myStack.Push("fox")
    
    While myStack.Count <> 0
        myStack.Pop()
    End While


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Karen,

    I'm really curious. What is your goal in this forum. Points to be MVP?

    You've become simply a "bad teacher".

    Do you really think I could not make that simple code to gain those points?


    brrrrrrrrrrrrrr Cor





    Friday, February 2, 2018 12:14 PM
  • Do you mean something like this

    Dim myStack As New Stack()
    myStack.Push("The")
    myStack.Push("quick")
    myStack.Push("brown")
    myStack.Push("fox")
    
    While myStack.Count <> 0
        myStack.Pop()
    End While


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Karen,

    I'm really curious. What is your goal in this forum. Points to be MVP?

    You've become simply a "bad teacher".

    Do you really think I could not make that simple code to gain those points?


    brrrrrrrrrrrrrr Cor





    Maybe Karen paid no attention to your post Cor and just supplied the answer. Is there something wrong with that? I'll tell you what is wrong here. It is you NothingButAHam sheriff riding around on you broom-stick horse in these MSDN forums thinking you are some kind of a gift to the forums, kind of like Donald Trump. Are you related to that lunatic?

    Saturday, February 3, 2018 2:20 AM
  • Do you mean something like this

    Dim myStack As New Stack()
    myStack.Push("The")
    myStack.Push("quick")
    myStack.Push("brown")
    myStack.Push("fox")
    
    While myStack.Count <> 0
        myStack.Pop()
    End While


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Karen,

    I'm really curious. What is your goal in this forum. Points to be MVP?

    You've become simply a "bad teacher".

    Do you really think I could not make that simple code to gain those points?


    brrrrrrrrrrrrrr Cor




    Let's suppose they wanted to do something more than simply pop off the stack, then using the Clear method would not make sense. They were given choices, which one they pick is up to them. I have never been in this for points, only to educate. So they made a choice given both options, they is indeed their personal choice. If you had not responded I would had mentioned using the Clear method as a option.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Saturday, February 3, 2018 2:29 AM
    Moderator