Singleton instance

Monkey Forums/Monkey Programming/Singleton instance

KaaPex(Posted 2012) [#1]
Hy all.

Is it possible using Singleton pattern?

im trying using this method:
Method GetInstance : IView()
If ( _instance = Null ) Then
_instance = New View( )
Endif
Return _instance
End Method

But got error then call View.GetInstance():

Method 'GetInstance' cannot be accessed from here


Jesse(Posted 2012) [#2]
make _instance a global and make GetInstance a function and it should work.


vbnz(Posted 2012) [#3]
KaaPex
Try to do this,like...
[monkeycode]Class ST
Field a:Int=23
Global _instance:ST
Function Get:ST()
If _instance=Null Then
_instance=New ST()
Endif
Return _instance
End function
End Class
Function Main:Int()
'Example
Local s:ST=ST.Get()
Print s.a
s.a=34
Local s1:ST=ST.Get()
Print s1.a
End Function[/monkeycode]


vbnz(Posted 2012) [#4]
Jesse,you are ahead!:D


KaaPex(Posted 2012) [#5]
It's work! Thanks