Stacks in Classes Problem

Monkey Forums/Monkey Programming/Stacks in Classes Problem

googlemyname(Posted 2014) [#1]
Hello all
I made a Class with Stacks inside. if I use 2 declarations of Classes With MyClass.Insert the first Object get overwritten by the second for some reason.

Here is an example of it
[Code]
Class MyApp Extends App
Field FirstObj:MyClass
Field SecondObj:MyClass

Method OnCreate()
FirstObj.myStack.Push(1.0)
SecondObj.myStack.Insert(0,2.0)
End
End

Class MyClass
Global myStack:Stack<Float>=New FloatStack
End
[/Code]

Is it a Bug or did i something wrong? I want to have every Object with its own seperate Stack.

Thanks in advice

Greetings


Gerry Quinn(Posted 2014) [#2]
They shouldn't get overwritten. But they will end up in reverse order if you do it that way.

After the push the stack contains < 1.0 >

After the insert, it contains < 2.0, 1.0 >. You moved the element that was at 0 to position 1, and put a new element at position 0.

If you want them in order < 1.0, 2.0 >, you should Push both (in the same order). Pushing 2.0 will add an extra element at the end of the stack, and place 2.0 there, leaving the 1.0 where it was.

In general, if you are using Insert a lot, a Stack is probably - there are always exceptions - the wrong data structure. A stack is like an array that auto-resizes if it fills up. So usually you are growing it using Push.


googlemyname(Posted 2014) [#3]
Well thats what i got...But shouldn't be the Stack of FirstObj an other than the one from the SecondObj? Why does the SecondObj influence the FirstObj?


dawlane(Posted 2014) [#4]
Your using a global variable in MyClass. It will treat it as a static variable, so any object created with MyClass will share the same data in the global variable.


googlemyname(Posted 2014) [#5]
Edit: Corrected Code(MyStack:Stach to MyStack:Stack)

Ok
doing this the following way i get a memory acces violation error

Class MyApp Extends App
       Field FirstObj:MyClass 
       Field SecondObj:MyClass

       Method OnCreate()
              FirstObj.Add(1.0)
              SecondObj.Add(2.0)
       End
End

Class MyClass
       Field MyStack:Stack<float>=New FloatStack()
       Method Add(v:Float)
              MyStack.Push(v) '<-- Error line
       End
End


Any other way to do this?


dawlane(Posted 2014) [#6]
If the code you posted was straight from your IDE then I would suggest using MyStack:Stack instead of MyStack:Stach and you haven't created the objects in the OnCreate with new.


sereschkin(Posted 2014) [#7]
Why not New Stack<Float>() ?


dawlane(Posted 2014) [#8]
This should help you out
Import mojo

Class MyApp Extends App
	
	' Three ways to initialise objects
	Field FirstObj:MyClass		' A definition that can be assigined later
	Field SecondObj:MyClass = MyClass.Create(1.0)	' Use a static function that create an object with pre-initialise with a value.
	Field ThirdObj:MyClass = New MyClass	' Create an empty object
	
	Method OnCreate()	
		FirstObj = MyClass.Create(2.0)	' Use a static function that create an object with pre-initialise with a value.
		
		' The next four lines require that an onbject exists using one of the methods above.
		ThirdObj.Add(3.0)
		FirstObj.Add(4.0)
		SecondObj.Add(50.0)
		ThirdObj.Add(30.0)
	End

	Method OnRender()
		' Call the object methods to pop items off the stack and display them
		FirstObj.Show("First Object")
		SecondObj.Show("Second Object")
		ThirdObj.Show("Thrid Object")
	End
End

Class MyClass
	Field MyStack:Stack<Float> = New FloatStack()
	'Field MyStack:= New Stack<Float>()		'Shorthand
	
	' Over ride the default New constructor 
	Method New(v:Float)
		MyStack.Push(v)
	End
	
	' Add an element to the stack  
	Method Add(v:Float)
		MyStack.Push(v)
	End
	Method Show(s:String)
		' Pop an element off the stack and display
		Local v:Float
		If Not MyStack.IsEmpty() Then v = MyStack.Pop(); Print s + " " + v
	End
	
	' Static function to create and initialise an object instance
	Function Create:MyClass(v:Float)
		Return New MyClass(v)
	End
End

Function Main()
	New MyApp()
End



googlemyname(Posted 2014) [#9]
@Sereschkin same problem just inverted

Thanks for help will try it later


Jesse(Posted 2014) [#10]
Class MyApp Extends App
       Field FirstObj:MyClass = New MyClass	'**********
       Field SecondObj:MyClass = New MyClass   '**********

       Method OnCreate()
              FirstObj.Add(1.0)
              SecondObj.Add(2.0)
       End
End

Class MyClass
       Field MyStack:Stack<float>=New FloatStack()
       Method Add(v:Float)
              MyStack.Push(v) '<-- Error line
       End
End