Building .NET Managed Components for COM+



Sample component coding


Imports System.ComponentModel

 _
Public Class GenericContact : Inherits Component
    ' Properties that are generic to all contacts
    Private mstrContactName As String
    Private mstrContactType As String

    ' Public property that exposes the contact name
     _
    Public Property ContactName() As String
    Get
        Return (mstrContactName)
    End Get
    Set(ByVal Value As String)
        mstrContactName = Value
    End Set
    End Property

    ' Public property that exposes the contact type
     _
    Public Property ContactType() As String
    Get
        Return (mstrContactType)
    End Get
    Set(ByVal Value As String)
        mstrContactType = Value
    End Set
    End Property

    ' Default constructor for the contact
    Public Sub New()
        Me.ContactName = "MSDN India"
        Me.ContactType = "Web Site"
    End Sub

    ' Constructor that accepts parameters for the contact
    Public Sub New(ByVal pContactName As String, _ 
        ByVal pContactType As String)
        mstrContactName = pContactName
        mstrContactType = pContactType
    End Sub
End Class




Note that in the above example, we have used properties that expose the various attributes of the class and it is on these properties that we apply the attributes. After you have placed this code in the editor, compile it using the Build menu. This will generate an assembly that we can then reference in the VS.NET IDE. To add this component in the IDE do the following steps (for example, on a Windows Form projecct): In the Visual Studio .NET toolbox, select the General tab, right click and choose Add/Remove Items (I�m using Everett Final Beta, in earlier versions of VS.NET, this option might be called Customize Toolbox). Choose the .NET Framework Components tab and click on the [Browse] button to locate the assembly that you created in the example above. After locating the assembly, select it. The assembly that appears in the .NET Framework Components list and is selected. Click on the [OK] button. The General toolbox then displays the component. You can now drag and drop this component onto the design surface of any other project and the VS.NET IDE will automatically write the necessary initialization code (which can be seen by choosing the View Code option and then opening the automatically generated region). Also, you can use the Properties Window to set values for the various properties defined in the object.

Return to top of this page

To contact me (Author of this website and software), please E-mail me at:
jep1965@gmail.com

This page last updated Aug 28 2004








Hosted by www.Geocities.ws

1