5,427,303 members and growing! (18,117 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » Mobile Development » General     Intermediate

Singleton Design Pattern with the Compact Framework

By JSaunders

Create single instances of forms using the Compact Framework.
VB, Windows, .NET CF, .NET 1.1, CE .NET 4.2, PocketPC 2002, Win Mobile, .NET, MobileVisual Studio, VS.NET2003, Dev

Posted: 9 Feb 2005
Updated: 9 Feb 2005
Views: 18,061
Bookmarked: 7 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
5 votes for this Article.
Popularity: 2.24 Rating: 3.20 out of 5
1 vote, 20.0%
1
0 votes, 0.0%
2
2 votes, 40.0%
3
1 vote, 20.0%
4
1 vote, 20.0%
5

Introduction

There may be times when you want to be sure that there is only one instance of a form within your Compact Framework application. You may also need to be able to pass data between open forms. This is why we use the singleton design pattern. You may have used this in a standard Windows application, but a Compact Framework application is a bit trickier.

Preparing the Form

The first thing that you will need to do is add an explicit implementation of the IDisposable interface. This is something that you do not have to do with a Windows form, however it will be necessary with a Compact Windows Form.

Public Class MyForm
Inherits System.Windows.Forms.Form
Implements IDisposable
....

Next, you will want to change the constructor of the form to Private. This makes sure that you cannot mistakenly create a new instance of the form from anywhere else:

Private Sub New()
MyBase.New()
'This call is required by the Windows Form Designer. 

InitializeComponent()
'Add any initialization after the InitializeComponent() call 

End Sub

Go ahead and remove the Protected Overloads Overrides Dispose method while you're there.

Next, you'll need to add two internal members:

Private handle As IntPtr
Private Shadows disposed As Boolean = False

*You have to declare the disposed member as Shadows because it would conflict with a member of the base class component.

Finalization

Add the necessary finalization methods:

#Region " Finalization "
Public Overloads Sub Dispose() Implements 
IDisposable.Dispose
Dispose(True)
' Take yourself off of the finalization queue

' to prevent finalization code for this object

' from executing a second time.

GC.SuppressFinalize(Me)
End Sub
Protected Overridable Overloads Sub Dispose(ByVal disposing As Boolean)
' Check to see if Dispose has already been called.

If Not (Me.Disposed) Then
' If disposing equals true, dispose all managed

' and unmanaged resources.

If (disposing) Then
' Dispose managed resources.

End If
' Release unmanaged resources. If disposing is false,

' only the following code is executed.

handle = IntPtr.Zero
' Note that this is not thread safe.

' Another thread could start disposing the object

' after the managed resources are disposed,

' but before the disposed flag is set to true.

' If thread safety is necessary, it must be

' implemented by the client.

End If
Me.Disposed = True
End Sub
' This Finalize method will run only if the

' Dispose method does not get called.

' By default, methods are NotOverridable.

' This prevents a derived class from overriding this method.

Protected Overrides Sub Finalize()
' Do not re-create Dispose clean-up code here.

' Calling Dispose(false) is optimal in terms of

' readability and maintainability.

  Dispose(False)
End Sub
'You'll need to handle the Closed event to ensure that the

'form is disposed when closed.

Private Sub MyForm_Closed(ByVal sender As Object, _
  ByVal e As System.EventArgs) Handles MyBase.Closed
    Me.Dispose()
End Sub
#End Region

Shared Instance

Now, the last part. You'll need to add an Instance method to act as your shared entry point for the form. This should go directly after the constructor.

...
End Sub
Private Shared _Instance As MyForm = Nothing

Public Shared Function Instance() As MyForm
 If _Instance Is Nothing OrElse _Instance.disposed = True Then
   _Instance = New MyForm
 End If
 _Instance.BringToFront()
 Return _Instance
End Function

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

JSaunders


My name is John Saunders and I am a .Net/Java Software Developer for a consulting group in Charlotte, North Carolina by day, and consultant to several startups by night. I work primarily with C# ASP.Net and SQL Server, but I also do a lot of Windows forms, Compact Framework, J2EE, and workflow development as well. Certified MCP, MCDST, MCAD & MCSD.Net and, oh yea, I also jump out of airplanes. Smile
Occupation: Web Developer
Location: United States United States

Other popular Mobile Development articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
  (Refresh) 
Subject  Author Date 
-- There are no messages in this forum --

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 9 Feb 2005
Editor: Rinish Biju
Copyright 2005 by JSaunders
Everything else Copyright © CodeProject, 1999-2008
Web15 | Advertise on the Code Project