0

I have a component which implements an IExtenderProvider. One property contains settings which should be the same for each instance of the component the user uses within his project. (And also editable at each instance)

Whats the common way to share information over several instances of a user control at design time? To manually write it to the app.config is no solution because I need a GUI for this setting.

Thanks in advance

Findnix

The mentioned static property works great if the property is just needed during design time, but in my case the property is also needed during runtime. In this case each instance sets the value which was set during its design time to the static property. Which means that the value depends on the initialization order of the instances.

Example:

At Design Time:

Adding the Component to form 1 and set the value to "Form1"

Adding the Component to form 2 and set the value to "Form2"

The value now is "Form2" in the designer for both instances, but at runtime if both forms are loaded the value is allways the one which was loaded as last one.

This is because the designer does not change the initialization value for the instances.

Findnix
  • 1
  • 2
  • You can store the property value not in an instance variable, but in a static one. That will definitely help to share the same value between instances. – Max Shmelev Nov 02 '12 at 15:50

1 Answers1

0

To make a property that is common to all instances and per-instance, you can do something like this:

class MyObject
{
    // static members are common to all instances
    static string DefaultProperty1 = "Some Default";

    private string _Property1;
    public string Property1
    {
        get
        {
            // If the backing field is NULL, return the default
            if (_Property1 == null)
                return DefaultProperty1;
            // Otherwise, return the per-instance value
            return _Property1;
        }
        set { _Property1 = value; }
    }
}

DefaultProperty1 could also be a property if you want that to be changable.

Tergiver
  • 14,171
  • 3
  • 41
  • 68