Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Exposing Properties of Contained Objects

Exposing Properties of Contained Objects

Visual Studio .NET 2003

Your component can act as a container for other objects, such as other components or controls. This means that the container logically encloses a contained object, and the container serves as an additional layer of functionality between the contained object and the client code. An example of this relationship might be a form and a control, where the control is contained in the form. In Visual Studio designers, a contained object is by default private, and thus cannot be accessed by the developer. If you want to make properties of these contained objects available to users of your component, you must expose the properties. A property of a contained object is exposed by creating a property in the container, and using the Get and Set accessors of that property to effect the change in the private property of the contained object.

Consider a hypothetical component with a contained object named MyComponent. In this example, when the user requests the ContainedText property, the value stored in the Text property of MyComponent is delivered. When the user assigns a value to this property, that value is automatically passed to the Text property of MyComponent and the Set code will execute, changing the Text property of MyComponent.

The following example shows how to expose the Text property of the contained object:

' Visual Basic
Public Property ContainedText() as String
   Get
      Return MyComponent.Text
   End Get
   Set(Value as String)
      MyComponent.Text = Value
   End Set
End Property

// C#
public string ContainedText
{
   get   
   {
      return MyComponent.Text;
   }
   set
   {
      MyComponent.Text = value;
   }
}

To expose a property of a constituent control

  1. In the Code Editor for your user control, create a public property. For details, see Property Implementation in Components.
  2. In the Get section of the property, write code that retrieves the value of the property you want to expose.
  3. In the Set section of the property, write code that passes the value of the property to the exposed property of the constituent control.

See Also

Implementing Properties, Methods, Members, and Events in Components | Exposing Properties of Constituent Controls

Show:
© 2015 Microsoft