[Mono-osx] Global Objects

Fischer Tobias T.Fischer at servonic.de
Mon Sep 26 03:58:38 EDT 2011


you could implement your global object as a singleton.
Then you could access your instance from everywhere you want.

Here a small example of a singleton implementation and usage:

public class Foo
	private static Foo _instance = null;

	public static Foo Instance()
		if (_instance == null)
			_instance = new Foo();
		return _instance;

	public void DoSomething()

public class Bar
	public void Compute()

Hope that helped.

Best regards,

-----Original Message-----
From: mono-osx-bounces at lists.ximian.com
[mailto:mono-osx-bounces at lists.ximian.com] On Behalf Of Andrew Ayre
Sent: Samstag, 24. September 2011 20:35
To: mono-osx
Subject: [Mono-osx] Global Objects

How do I declare global instances of objects?

When using C# and .NET I would do something like this:

public static program {
  public static Foo MyFoo = new Foo();

  static void main() {
    MainForm = new MainForm(MyFoo);

however with Mono/MonoMac the main function calls NSApplication.Main and
doesn't directly create any windows. How would I pass an instance of
MyFoo to the main window?

Note: I am trying to avoid any references to MainClass in my
windows/window controllers as that creates a tight coupling. I want to
reuse the window classes in other situations hence the desire for loose

Is what I want possible with MonoMac?

thanks, Andy

PGP Key ID: 0xDC1B5864
Mono-osx mailing list
Mono-osx at lists.ximian.com

More information about the Mono-osx mailing list