Monday, February 28, 2011

check if an object has been instantiated in php?

You have to check a variable to see whether it contains an object of the
right type

Thus
-------------------------------------------------------------
Dim X as clsContract

If X is nothing then
MessageBox.Show("There is No Object here")
Endif
X = New clsContract

If Not (X is nothing) then
MessageBox.Show("There is an Object here")
Endif
-------------------------------------------------------------


However the variable you test can be a shared variable within the class.
This is an example of the 'singleton' pattern.

Thus
-------------------------------------------------------------
Public Class clsContract
Private Sub New()
End Sub
Private Shared mSingleton as clsClass
Public Shared Function GetSingleton as clsContract
If mSingleton is nothing then
mSingleton = New clsContract
Endif
return mSingleton
End function
Public Shared function Exists() as boolean
return not (mSingleton is nothing)
End function
End Class
-------------------------------------------------------------

Your button code might then use this
-------------------------------------------------------------
Dim x as clsContract = clsContract.GetSingleton()
-------------------------------------------------------------

This may or may not be what you mean.

2 comments: