none
Help with stack for cell co-ordinates RRS feed

  • Question

  • I am trying to teach myself VB (with a lot of help from people here). Currently playing with stacks. I can make a stack and push and pop variables for strings, but when I try to use a stack to store coordinates it doesn't work. Is there a simple way of using a stack with coordinates, or do  I have to convert the coordinates to a string in some way and then convert them back if I need to use them as coordinates later?  The attached code shows the sort of thing I want to do. This code works with days of the week, but when I change my variables to cells e.g. dim cell1(0,1) as point or string  - the code doesn't run. 

    All advice and explanations gratefully received - the simpler the better

    Public Class Form1
        Dim mystack As New Stack
      
        Sub createstackcells()
            Dim cell1(0, 1) As String
            Dim cell2(0, 2) As String
            Dim cell3(1, 1) As String
            mystack.Push(cell1)
            MsgBox(mystack.Peek)
            mystack.Push(cell2)
            mystack.Push(cell3)
        End Sub
        Sub createstackdays()
            Dim day1 As String
            Dim day2 As String
            Dim day3 As String
            day1 = "Monday"
            day2 = "Tuesday"
            day3 = "Wednesday"
    
            mystack.Push(day1)
            MsgBox(mystack.Peek)
            mystack.Push(day2)
            MsgBox(mystack.Peek)
            mystack.Push(day3)
            MsgBox(mystack.Peek)
        End Sub
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            createstackdays()
            ' createstackcells()
            MsgBox(mystack.Pop)
    
        End Sub
    End Class


    Monday, December 18, 2017 12:45 PM

Answers

  • Hi

    Don't know if this is of any help. You seem to want to work with pairs of Integer values (co-ordinates).

    Here is some code that may assist. It Pushes Integer pairs onto a Stack, then Peeks at the top object, and then Pops the top most object - remember, working with arrays here.

    	Dim mystack As New Stack(Of Integer())
    	mystack.Push({1, 2})
    	mystack.Push({2, 2})
    	mystack.Push({33, 44})
    	mystack.Push({0, 0})
    	mystack.Push({1234, 4321})
    	MessageBox.Show(mystack.Peek()(0).ToString)
    	MessageBox.Show(mystack.Peek()(1).ToString)
    
    	Dim a() As Integer = mystack.Pop
    	MessageBox.Show(a(0).ToString)
    	MessageBox.Show(a(1).ToString)
    


    Regards Les, Livingston, Scotland

    • Proposed as answer by Frank L. Smith Monday, December 18, 2017 9:47 PM
    • Marked as answer by CrazyMum123 Tuesday, December 19, 2017 2:39 PM
    Monday, December 18, 2017 2:09 PM
  • I can make a stack and push and pop variables for strings, but when I try to use a stack to store coordinates it doesn't work. Is there a simple way of using a stack with coordinates, or do  I have to convert the coordinates to a string in some way and then convert them back if I need to use them as coordinates later?

    The framework provides a class that you can use to represent a pair of integers.   You can also create your own class, or use an existing class like Point.

        Public Sub createstackcells()
            Dim cell1 As New Tuple(Of Integer, Integer)(0, 1)
            Dim cell2 As New Tuple(Of Integer, Integer)(0, 2)
            Dim cell3 As New Tuple(Of Integer, Integer)(1, 1)
            mystack.Push(cell1)
            MsgBox(mystack.Peek.ToString)
            mystack.Push(cell2)
            mystack.Push(cell3)
        End Sub
    
        Dim mystack As New Stack(Of Tuple(Of Integer, Integer))
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            createstackcells()
        End Sub


    • Edited by Acamar Monday, December 18, 2017 8:06 PM sp
    • Proposed as answer by Frank L. Smith Monday, December 18, 2017 9:47 PM
    • Marked as answer by CrazyMum123 Tuesday, December 19, 2017 2:39 PM
    Monday, December 18, 2017 7:14 PM

All replies

  • Hi

    Don't know if this is of any help. You seem to want to work with pairs of Integer values (co-ordinates).

    Here is some code that may assist. It Pushes Integer pairs onto a Stack, then Peeks at the top object, and then Pops the top most object - remember, working with arrays here.

    	Dim mystack As New Stack(Of Integer())
    	mystack.Push({1, 2})
    	mystack.Push({2, 2})
    	mystack.Push({33, 44})
    	mystack.Push({0, 0})
    	mystack.Push({1234, 4321})
    	MessageBox.Show(mystack.Peek()(0).ToString)
    	MessageBox.Show(mystack.Peek()(1).ToString)
    
    	Dim a() As Integer = mystack.Pop
    	MessageBox.Show(a(0).ToString)
    	MessageBox.Show(a(1).ToString)
    


    Regards Les, Livingston, Scotland

    • Proposed as answer by Frank L. Smith Monday, December 18, 2017 9:47 PM
    • Marked as answer by CrazyMum123 Tuesday, December 19, 2017 2:39 PM
    Monday, December 18, 2017 2:09 PM
  • I can make a stack and push and pop variables for strings, but when I try to use a stack to store coordinates it doesn't work. Is there a simple way of using a stack with coordinates, or do  I have to convert the coordinates to a string in some way and then convert them back if I need to use them as coordinates later?

    The framework provides a class that you can use to represent a pair of integers.   You can also create your own class, or use an existing class like Point.

        Public Sub createstackcells()
            Dim cell1 As New Tuple(Of Integer, Integer)(0, 1)
            Dim cell2 As New Tuple(Of Integer, Integer)(0, 2)
            Dim cell3 As New Tuple(Of Integer, Integer)(1, 1)
            mystack.Push(cell1)
            MsgBox(mystack.Peek.ToString)
            mystack.Push(cell2)
            mystack.Push(cell3)
        End Sub
    
        Dim mystack As New Stack(Of Tuple(Of Integer, Integer))
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            createstackcells()
        End Sub


    • Edited by Acamar Monday, December 18, 2017 8:06 PM sp
    • Proposed as answer by Frank L. Smith Monday, December 18, 2017 9:47 PM
    • Marked as answer by CrazyMum123 Tuesday, December 19, 2017 2:39 PM
    Monday, December 18, 2017 7:14 PM
  • Never heard of TUPLES before but now I've had a chance to take a look, I think this is an elegant way of doing it, thank you

    Tuesday, December 19, 2017 2:41 PM