Question Get an Instance of an object

Darrell86

New member
Joined
Oct 14, 2008
Messages
2
Programming Experience
1-3
Hello

I am having a problem that i would be really greatful for any direction with.

I have created a vb.net project (windows forms application), and added a new class to it.

The purpose of the class is to keep a single instance of a collection of "Games" in memory. Therefore this class uses the singleton pattern to acheive this. I have put the code below.

VB.NET:
Public Class GamesFactory

Shared _Games As List(Of IGames)

Shared Function GetGames() As List(Of IGames)
  If _Games Is Nothing Then
    _Games = New List(Of IGames)
  End If
  Return _Games
End Function

Shared Function AddGame(ByVal Game As IGame) As Integer

  GetGames().Add(Game)

End Function

End Class

When the windows forms application loads, populates this collection with a few games.

This works fine so far.

Here is the problem..

When i add another project to this solution - ie a Web Service for example.

I add a reference for the windows forms application to my Web Service project.

If i start my windows forms application running - then start my web service running, and from the webservice i call the "GamesFactory.GetGames" method - i get back a completely new instance of the Games collection, even though i know one allready exists in memory.

I am not sure how i can force this collection to be a single instance in memory, and retrieve that same instance from othe projects within the solution!

Please Please help ;)

Many Thanks

Darrell
 
Let me get this right.

Youve made a shared variable, which within the context of this application domain is single instance, and youre starting an exe.

And now youre making another project, which will build a different exe, which will start with it's own protected memory space etc and ts own domain...

And you want them to share?


In other words: Suppose you created a Shared TextBox in your app, and built the EXE, and then started 10 copies of it, so you can see 10 windows open on the desktop..
And youre expecting to type into 1 textbox, and the other 9 also update to show the same value because they are sharing the same memory?


Umm.. OK, that kinda throws away all microsoft's efforts at protected memory for the last 20 years ;)

-

You can't do it. You'll have to implement remoting or some other kind of IPC and have any child or subsequent instances of any app, contact your main app and ask for the data.. IPC is not as simple as declaring a shared variable in your app!
 
Back
Top