Thanks for this. I'm starting to understand and I had a go but I was still
struggling a little. I couldn't access to object model of Microstation
through this singleton class that I built. So that I can understand this I
am starting with a basic example, but what might that class look like if it
were to be added to the following simple app?
Russ
Public Class Form1
Inherits System.Windows.Forms.Form
Private oWinWord As Word.Application
#Region " Windows Form Designer generated code "
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Word isn't running so start it
oWinWord = New Word.Application
oWinWord.Visible = True
Me.Label1.Text = "There are " & CStr(oWinWord.Documents.Count) & "
documents open"
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
oWinWord = New Word.Application
oWinWord.Documents.Add()
Me.Label1.Text = "There are " & CStr(oWinWord.Documents.Count) & "
documents open"
End Sub
End Class
> Even better (VB.Net specific):
> http://www.ondotnet.com/pub/a/dotnet/2002/11/11/singleton.html
Jonas Pohlandt - 01 Oct 2004 13:08 GMT
Well, the purpose of the singleton pattern is to make sure that you are
allways working with only one instance of a class.
I thought this was what you wanted. Applied to your word example it would be
something like this.
public class WordAppSingleton
private shared _myInstance as Word.Application
public shared function GetInstance() as Word.Application
if _myInstance is nothing
_myInstance = New Word.Application 'TODO: Add check if App is
allready running
end if
return _myInstance
end function
end class
Now, if you want to work with the instance of your word application you
would use the shared function WordAppSingleton.GetInstance() to get it, like
this:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Word isn't running so start it
dim oWinWord as Word.Application = WordAppSingleton.GetInstance()
oWinWord.Visible = True
Me.Label1.Text = "There are " & CStr(oWinWord.Documents.Count) & "
documents open"
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
dim oWinWord as Word.Application = WordAppSingleton.GetInstance()
oWinWord.Documents.Add()
Me.Label1.Text = "There are " & CStr(oWinWord.Documents.Count) & "
documents open"
End Sub
So you won't be needing a global oWinWord variable anymore. Use local
variables and WordAppSingleton.GetInstance().
Is this what you need?
Russ Green - 01 Oct 2004 16:03 GMT
That's very clear, thank you so much
> Well, the purpose of the singleton pattern is to make sure that you are
> allways working with only one instance of a class.
[quoted text clipped - 43 lines]
> variables and WordAppSingleton.GetInstance().
> Is this what you need?