473,898 Members | 3,068 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Singleton Design Pattern & Object persistence

Hey guys, I've been hitting a brick wall with a problem I've come accross
in developing an application.

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.

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.
'Deserializatio n code in ProcessProject( ) Method

Dim fs As New FileStream(StrP athToProject, 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.D eserialize(fs), CXiNKProject)

bf = Nothing

'initialization code etc..."
'Singleton Class Code
Imports System
Imports System.Threadin g

Public Class CXiNKSingleton
Implements IDisposable

#Region "Properties "
Private Shared __InstMutex As Mutex
Private Shared __InstanceCount As Integer = 0
Private Shared __SingletonProj ect As CXiNKProject ' = Nothing
Private Shared __IsActive As Boolean = False
#End Region

#Region "Get/Set Properties"
Public Shared Property IsActive() As Boolean
Return __IsActive
End Get
Set(ByVal Value As Boolean)
__IsActive = Value
End Set
End Property

Private Shared Property InstMutex() As Mutex
Return __InstMutex
End Get
Set(ByVal Value As Mutex)
__InstMutex = Value
End Set
End Property

Public Shared Property InstanceCount() As Integer
Return __InstanceCount
End Get
Set(ByVal Value As Integer)
__InstanceCount = Value
End Set
End Property

Public Shared ReadOnly Property GetInstance() As CXiNKProject
If __SingletonProj ect Is Nothing Then
InstMutex = New Mutex
InstMutex.WaitO ne()
If __SingletonProj ect Is Nothing Then
Console.WriteLi ne("No existing instance of CXiNKProject
Console.WriteLi ne("Creating new instance of
CXiNKProject... ")

__SingletonProj ect = New CXiNKProject
IsActive = True
InstanceCount += 1
InstMutex.Relea seMutex()
End If
End If

Console.WriteLi ne("Returning new/existing instance of
CXiNKProject: " & InstanceCount() )
Console.WriteLi ne()
Return __SingletonProj ect
End Get
End Property
#End Region

#Region "Constructo r/Destructor"
Protected Sub New()
'override default constructor
End Sub

Public Sub Dispose() Implements System.IDisposa ble.Dispose
'reset everything
IsActive = False
InstanceCount = 0
__SingletonProj ect = Nothing
Console.WriteLi ne("Disposing of object: " & "IsActive " & IsActive()
& " InstanceCount: " & InstanceCount() )
End Sub
#End Region
End Class

Message posted via http://www.dotnetmonster.com
Nov 21 '05 #1
2 1808
Problem is here...

ObjProject = CXiNKSingleton. GetInstance()
'deserialize & cast to the object
ObjProject = DirectCast(bf.D eserialize(fs), CXiNKProject)
You get a reference to the one and only singleton and store it in a
local variable. Then you deserialize a stream to the same local
object, overwriting the local reference to the singleton.

But then the function exists and the local reference is released.

What you need is a SetInstance() method or some other method
(preferrably internal to the singleton class) that sets the singleton
instance to be the deserialized object.


B-Line is now hiring one VB.NET developer for WinForms + WebServices
position with strong possibility of ASPX in future. Seaking mid to
senior level developer. For information or to apply e-mail
Nov 21 '05 #2
Wicked! thanks, I'm going to update my classes to handle this problem. I
really appreciate the assitance!

Message posted via http://www.dotnetmonster.com
Nov 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

by: Tim Clacy | last post by:
Is there such a thing as a Singleton template that actually saves programming effort? Is it possible to actually use a template to make an arbitrary class a singleton without having to: a) explicitly make the arbitrary class's constructor and destructor private b) declare the Singleton a friend of the arbitrary class
by: Alicia Roberts | last post by:
Hello everyone, I have been researching the Singleton Pattern. Since the singleton pattern uses a private constructor which in turn reduces extendability, if you make the Singleton Polymorphic what sort of problems/issues should be considered? Also, I see that a singleton needs to be set up with certain data such as file name, database URL etc. What issues are involved in this, and how would you do this? If someone knows about the...
by: Mountain Bikn' Guy | last post by:
Is the second version shown below better? I couldn't locate enough info about in order to tell. 1.. My commonly used singleton pattern implementation looks like this (it was inspired by Eric Gunnerson's book): private static volatile MyClass singleton = null; private static object sync = new object();//for static lock public static MyClass Instance {
by: Sharon | last post by:
I wish to build a framework for our developers that will include a singleton pattern. But it can not be a base class because it has a private constructor and therefore can be inherit. I thought maybe a Template can be use for that, but C# does not support Templates (will be C# generics in mid 2005). Does anyone have a solution on how the singleton pattern can be written, in C#, as a framework/ infrastructure class, so users can use this...
by: Robert W. | last post by:
At the beginning of my C# days (about 6 months ago) I learned about the Singleton pattern and implemented for Reference data, such as the kind that appears in an Options dialog box. My Singleton code looks like this: public sealed class Reference { private static readonly Reference instance = new Reference(); // Make the default constructor private, so that nothing can directly create it.
by: Eric | last post by:
I am implementing a variation on the Singleton design pattern, that allows up to 8 objects of a class to be instantiated, and returns a null pointer for anything more than 8. I am running into a compile problem with GNU g++. Here is the code: /******************** FILE sample.h ********************/
by: baba | last post by:
Hi all, I'm quite new to C#. I am trying to implement some basics reusable classes using this language and the .NET Framework technology. What I'm trying to do now is to implement a singleton class. I did have a look at the Microsoft "Patterns and Practices" article "Implementing Singleton in C#" (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnpatterns/html/ImpSingletonInCsharp.asp)
by: weaknessforcats | last post by:
Design Pattern: The Singleton Overview Use the Singleton Design Pattern when you want to have only one instance of a class. This single instance must have a single global point of access. That is, regardless of where the object is hidden, everyone needs access to it. The global point of access is the object's Instance() method. Individual users need to be prevented from creating their own instances of the Singleton.
by: Lie | last post by:
This is probably unrelated to Python, as this is more about design pattern. I'm asking your comments about this design pattern that is similar in functionality to Singleton and Borg: to share states. I'm thinking about this design pattern (I don't know if anyone has ever thought of this pattern before): class OddClass(object): def __init__(self): global OddClass
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.