Hi Joe,
You can use the singleton pattern to ensure that only a single instance of
your server component may be used:
sealed class GlobalServer
{
public static readonly GlobalServer Instance = new GlobalServer();
public object GlobalState
{
get { return globalState; }
set { globalState = value; }
}
private static object globalState;
// remove the private constructor (or add a public one)
// if you want to allow creation of instances that aren't
// shared (see comments below)
private GlobalServer() { }
}
In your "client" components, you don't have to assign them any reference.
Just use GlobalServer.In stance directly or wrap it in a property:
class Client : Component
{
public GlobalServer Server
{
get
{
return GlobalServer.In stance;
}
}
}
If you need to be able to assign different instances of GlobalServer to your
Client component then simply add a "set" accessor with a local field in
which to store the reference. To assign an instance in the Form designer
using the Properties window, your GlobalInstance component would have to
derive from Component as well and must be added to the Form, but then it
will no longer be a shared (global) instance. You'll also have to indicate
to the PropertyGrid which Types are allowable for your Server property. You
can use a base editor that I made for one of my projects:
// 2.0 framework code
using System;
using System.Collecti ons.Generic;
using System.Text;
using System.Componen tModel.Design;
using System.Componen tModel;
using System.Windows. Forms;
namespace YourLibrary
{
public abstract class ComponentSelect orEditor<T: ObjectSelectorE ditor
where T : class, IComponent
{
public ComponentSelect orEditor()
{
}
protected override void FillTreeWithDat a(Selector selector,
System.Componen tModel.ITypeDes criptorContext context, IServiceProvide r
provider)
{
selector.Clear( );
selector.AddNod e("(none)", null, null);
Type baseType = typeof(T);
IReferenceServi ce referenceServic e =
provider.GetSer vice(typeof(IRe ferenceService) ) as IReferenceServi ce;
foreach (IComponent component in context.Contain er.Components)
{
if (baseType.IsAss ignableFrom(com ponent.GetType( )))
{
string name = null;
if (component != null && component.Site != null)
name = component.Site. Name;
else if (referenceServi ce != null)
name = referenceServic e.GetName(compo nent);
else
throw new InvalidOperatio nException("The re is no service available to
retrieve the name of one or more components.");
selector.AddNod e(name, component, null);
}
}
}
}
}
With this base editor you can derive an editor that will allow properties to
bind to "GlobalServ er" components:
public sealed class ServerSelectorE ditor
: ComponentSelect orEditor<Global Server // base component Type
{
// no implementation required :)
}
Note: GlobalServer should be renamed to Server since it's not global anymore
The editor above can be assigned to your Server property like this:
class Client : Component
{
private GlobalServer server;
[Editor(typeof(S erverSelectorEd itor),
typeof(System.D rawing.Design.U ITypeEditor))]
[DefaultValue(nu ll)] // not req. but recommended
public GlobalServer Server
{
get { return server; }
set { server = value; }
}
}
NOTE: You may need to build the solution and then close and restart VS for
the changes to take affect, or else you might not see any GlobalServer
components in the list even though some have been added to the Form
designer.
You could code instances of GlobalServer to access only static resources, so
in a sense you could actually do what you wanted using the editor above,
however I'd recommend using the first scenario for that and foregoing the
designer support if you don't actually need instances of GlobalServer.
--
Dave Sexton
"Joe" <jb*******@noem ail.noemailwrot e in message
news:et******** ******@TK2MSFTN GP04.phx.gbl...
Is it possible to have a component which is global to the entire
application? I need to have a single component act sort of like a server
which components in any of the forms can access.
For example if I drop a component on Form1 & Form2 and that component has
a property called Server, at design time I would like to be able to assign
the global component to the Server property.
I'm sure I'm asking for way too much...
Thanks for any help,
Joe