Simple question? Sharing data among tools and commands

731
8
Jump to solution
03-26-2013 07:12 AM
StevenHill1
New Contributor
I am customizing ArcMap using VS2008 with the Developer Kit for .NET.  I have created a C# project with some commands and tools and an associated toolbar.  I want to share some data among these items within the project (so that the data is available to each tool or command when it is executing), but I can't figure out how to do it.  I'm pretty sure this is a simple question, but it's got me stumped for now.  Can anyone give me some suggestions?
0 Kudos
1 Solution

Accepted Solutions
LeoDonahue
Occasional Contributor III
Somewhere in the event of the tool, initialize an object that stores that data.  As Jason suggested, you could make it a singleton.  It would only ever have one instance throughout the duration of your Add-in.

You should be able to access the instance of that object from other classes that represent your other tools / commands.  You will want to make sure that the other tools / commands know how to repsond when the instance has not been created.  In other words, check to see if the instance has been created, then you can call properties or methods that retrieve the data you want.

That is one option.

View solution in original post

0 Kudos
8 Replies
LeoDonahue
Occasional Contributor III
I have created a C# project with some commands and tools and an associated toolbar.  I want to share some data among these items within the project (so that  the data is available to each tool or command when it is executing)

This would be a design pattern, what you choose is up to you.

All commands and tools are in the same project?  Is your customization an Add-in?
0 Kudos
StevenHill1
New Contributor
All commands, tools and the toolbar are in the same project, which is an add-in (to be implemented via Tools/Customization).  As to design patterns, right now I have no choices - as I said, I am stumped, but I think I am missing something really obvious...
0 Kudos
JasonPike
Occasional Contributor
All commands, tools and the toolbar are in the same project, which is an add-in (to be implemented via Tools/Customization).  As to design patterns, right now I have no choices - as I said, I am stumped, but I think I am missing something really obvious...


You can try a singleton. Only one instance can exist in the AppDomain at a particular time. 

http://msdn.microsoft.com/en-us/library/ff650316.aspx
0 Kudos
LeoDonahue
Occasional Contributor III
Steven,

Can you share what you mean by "data"?   I know you want to share data between the tools and commands, but can you be specific?  Data from where?  Hardcoded?  From an external source?

Obviously you need a model that handles the logic for data that is shared between these tools and commands.  Where/When do you initialize such a model?  Do the tools and commands have a natural workflow?

Have you considered an application extension?

http://help.arcgis.com/en/sdk/10.0/arcobjects_net/conceptualhelp/0001/0001000004m1000000.htm
0 Kudos
StevenHill1
New Contributor
OK, here is an example of what I mean - one of the tools is going to generate, through user input on the map, an angle.  I want the value of the angle to be available to other commands and tools in the project.  How do I do that?
0 Kudos
LeoDonahue
Occasional Contributor III
Somewhere in the event of the tool, initialize an object that stores that data.  As Jason suggested, you could make it a singleton.  It would only ever have one instance throughout the duration of your Add-in.

You should be able to access the instance of that object from other classes that represent your other tools / commands.  You will want to make sure that the other tools / commands know how to repsond when the instance has not been created.  In other words, check to see if the instance has been created, then you can call properties or methods that retrieve the data you want.

That is one option.
0 Kudos
AlexanderGray
Occasional Contributor III
In ArcGIS desktop ArcObjects, the design I always have used for this is an ArcGIS desktop extension.  Singletons, in my opinion, are almost like global variables, so are static members on a class.  An ArcGIS extension can be used outside the assembly (by using COM interfaces) and offer a pattern that is easy to implement.  All your commands can get access to your extension through the Application class passed in during oncreate.  They are guaranteed only one instance per session and fit in with the design pattern of the application.
0 Kudos
StevenHill1
New Contributor
Thanks to all for the help - I have used a singleton class and it looks like it will do the job -still have a long way to go.  I understand that I could also make my set of tools etc and extension, but I'm trying to avoid that additional level of complexity.
0 Kudos