C
Chris Murphy via DotNetMonster.com
Hey guys, I've been hitting a brick wall with a problem I've come accross
in developing an application.
Background:
The application uses one primary class that I'm trying to implement with
the singleton pattern in mind. The design pattern seems to be working
properly (complex and simple variations of it) Serialzation/Deserialzation
seems to work okay.
I run into a problem when I attempt to load the file back into the
application. Deserialzation works fine and the object is restored, and I
print out it's properties using the object I deserialized it to.
Problem:
Problems occur when I try to use the deserialized object (contained &
referenced in the singleton class) from a different method. Printing the
object's properties reveals only a blank object (no property values
available -- other than defaults provided)
I'm quite tired atm, so I'm thinking it's something really stupid like the
scope of the object in the first method. Can anyone shed some light on this?
I feel like an idiot right now.
'---------------------------------------------------------------
'Deserialization code in ProcessProject() Method
'---------------------------------------------------------------
Dim fs As New FileStream(StrPathToProject, FileMode.Open, FileAccess.Read)
Dim bf As New BinaryFormatter
'CXiNKProject is the class/object I'm restoring
Dim ObjProject As CXiNKProject
'Creating a unique instance of the class
ObjProject = CXiNKSingleton.GetInstance()
'deserialize & cast to the object
ObjProject = DirectCast(bf.Deserialize(fs), CXiNKProject)
'cleanup
fs.Close()
bf = Nothing
'initialization code etc..."
'---------------------------------------------------------------
'---------------------------------------------------------------
'Singleton Class Code
'---------------------------------------------------------------
Imports System
Imports System.Threading
Public Class CXiNKSingleton
Implements IDisposable
#Region "Properties"
Private Shared __InstMutex As Mutex
Private Shared __InstanceCount As Integer = 0
Private Shared __SingletonProject As CXiNKProject ' = Nothing
Private Shared __IsActive As Boolean = False
#End Region
#Region "Get/Set Properties"
Public Shared Property IsActive() As Boolean
Get
Return __IsActive
End Get
Set(ByVal Value As Boolean)
__IsActive = Value
End Set
End Property
Private Shared Property InstMutex() As Mutex
Get
Return __InstMutex
End Get
Set(ByVal Value As Mutex)
__InstMutex = Value
End Set
End Property
Public Shared Property InstanceCount() As Integer
Get
Return __InstanceCount
End Get
Set(ByVal Value As Integer)
__InstanceCount = Value
End Set
End Property
Public Shared ReadOnly Property GetInstance() As CXiNKProject
Get
If __SingletonProject Is Nothing Then
InstMutex = New Mutex
InstMutex.WaitOne()
If __SingletonProject Is Nothing Then
Console.WriteLine("No existing instance of CXiNKProject
found...")
Console.WriteLine("Creating new instance of
CXiNKProject...")
__SingletonProject = New CXiNKProject
IsActive = True
InstanceCount += 1
InstMutex.ReleaseMutex()
End If
End If
Console.WriteLine("Returning new/existing instance of
CXiNKProject: " & InstanceCount())
Console.WriteLine()
Return __SingletonProject
End Get
End Property
#End Region
#Region "Constructor/Destructor"
Protected Sub New()
'override default constructor
End Sub
Public Sub Dispose() Implements System.IDisposable.Dispose
'reset everything
IsActive = False
InstanceCount = 0
__SingletonProject = Nothing
Console.WriteLine("Disposing of object: " & "IsActive " & IsActive()
& " InstanceCount: " & InstanceCount())
End Sub
#End Region
End Class
'---------------------------------------------------------------
in developing an application.
Background:
The application uses one primary class that I'm trying to implement with
the singleton pattern in mind. The design pattern seems to be working
properly (complex and simple variations of it) Serialzation/Deserialzation
seems to work okay.
I run into a problem when I attempt to load the file back into the
application. Deserialzation works fine and the object is restored, and I
print out it's properties using the object I deserialized it to.
Problem:
Problems occur when I try to use the deserialized object (contained &
referenced in the singleton class) from a different method. Printing the
object's properties reveals only a blank object (no property values
available -- other than defaults provided)
I'm quite tired atm, so I'm thinking it's something really stupid like the
scope of the object in the first method. Can anyone shed some light on this?
I feel like an idiot right now.
'---------------------------------------------------------------
'Deserialization code in ProcessProject() Method
'---------------------------------------------------------------
Dim fs As New FileStream(StrPathToProject, FileMode.Open, FileAccess.Read)
Dim bf As New BinaryFormatter
'CXiNKProject is the class/object I'm restoring
Dim ObjProject As CXiNKProject
'Creating a unique instance of the class
ObjProject = CXiNKSingleton.GetInstance()
'deserialize & cast to the object
ObjProject = DirectCast(bf.Deserialize(fs), CXiNKProject)
'cleanup
fs.Close()
bf = Nothing
'initialization code etc..."
'---------------------------------------------------------------
'---------------------------------------------------------------
'Singleton Class Code
'---------------------------------------------------------------
Imports System
Imports System.Threading
Public Class CXiNKSingleton
Implements IDisposable
#Region "Properties"
Private Shared __InstMutex As Mutex
Private Shared __InstanceCount As Integer = 0
Private Shared __SingletonProject As CXiNKProject ' = Nothing
Private Shared __IsActive As Boolean = False
#End Region
#Region "Get/Set Properties"
Public Shared Property IsActive() As Boolean
Get
Return __IsActive
End Get
Set(ByVal Value As Boolean)
__IsActive = Value
End Set
End Property
Private Shared Property InstMutex() As Mutex
Get
Return __InstMutex
End Get
Set(ByVal Value As Mutex)
__InstMutex = Value
End Set
End Property
Public Shared Property InstanceCount() As Integer
Get
Return __InstanceCount
End Get
Set(ByVal Value As Integer)
__InstanceCount = Value
End Set
End Property
Public Shared ReadOnly Property GetInstance() As CXiNKProject
Get
If __SingletonProject Is Nothing Then
InstMutex = New Mutex
InstMutex.WaitOne()
If __SingletonProject Is Nothing Then
Console.WriteLine("No existing instance of CXiNKProject
found...")
Console.WriteLine("Creating new instance of
CXiNKProject...")
__SingletonProject = New CXiNKProject
IsActive = True
InstanceCount += 1
InstMutex.ReleaseMutex()
End If
End If
Console.WriteLine("Returning new/existing instance of
CXiNKProject: " & InstanceCount())
Console.WriteLine()
Return __SingletonProject
End Get
End Property
#End Region
#Region "Constructor/Destructor"
Protected Sub New()
'override default constructor
End Sub
Public Sub Dispose() Implements System.IDisposable.Dispose
'reset everything
IsActive = False
InstanceCount = 0
__SingletonProject = Nothing
Console.WriteLine("Disposing of object: " & "IsActive " & IsActive()
& " InstanceCount: " & InstanceCount())
End Sub
#End Region
End Class
'---------------------------------------------------------------