Components in C#

Environment: C# .NET SDK Beta 2

The components creation and usage in C# is much more simple than earlier technologies like C++, ATL COM. As we know a Components represent all kinds of elements that pertain to the piecing together of software applications. Among other things, they may be simple files, or libraries loaded dynamically.

A component is a class with cleanup and containment. A component can be hosted in a container, and has the ability to query and get services from its container. Containment is logical and does not have to be visual. These components can be deployed in middle tier container as business components. Example database components deployed in middle tier.

In this article, you will see how to create a simple component in C# and how to use it in a client program. For an example I will use an Arithmetic component, which does addition.

We will create a component called csAddComp1 and package it into a dll (Dynamic Linked Library). This component has two properties and a method. Properties take input for the addition and method called Sum().

To create properties in C# you use the get and set accessors. The get accessor is used for getting (reading). The set accessor for setting (writing) the property.

In my example we have two properties, varI and varJ. These properties need to be written to and read from. To implement the reading and writing we need to use both get and set accessors for each property. I am using a namespace in my example int order to hide the classes being are creating inside. For easy understanding I made more comments in example code.

Component Program

using System;
namespace CompCS
{
 public class csAddComp1
 {
  private int i=0,j=0;
  public int varI
  {
    get { return i; } //this is property get for varI
    set { i=value; }  //this is property set for varI
  }
  public int varJ
  {
    get { return j; } // this is property get for varJ
    set { j=value; }  // this is property set for varJ
  }
  public int Sum()
  {
    return i+j; //this returns the sum of two variables
  }
 } //end of class
} //end of namespace

To package the component as dll there is slight change in usual compilation process. Its little complicated process when compared to normal stand-alone program compilation.

csc /out:csAddComp1.dll /target:library csAddComp1.cs

Here the /out switch to put the compiled component in the relative subdirectory and file for convenience. Likewise, we need the /target:library switch to actually create a DLL rather than an executable with a .dll file extension.

In client program we will use a simple keyword called using <namespace> <namespace>. Which will refer to the component.

Client Program 

using System;
using CompCS;

class clAddComp1
{
 public static void Main()
 {
   csAddComp1 addComp= new csAddComp1();
   addComp.varI=10; //property set for varI
   addComp.varJ=20; //property set for varJ

   //below property get for varI
   Console.WriteLine("variable I value : {0}",addComp.varI);

   //below property get for varJ
   Console.WriteLine("variable J value : {0}",addComp.varJ);

   // calling Sum(..) method
   Console.WriteLine("The Sum : {0}",addComp.Sum());
 } //end of Main
} // end of Class

Program Output :

C:\csharp\progs>clAddComp1 
variable I value : 10
variable J value : 20
The Sum : 30

Since the component is residing in csAddComp1.dll, the client program has to refer it at the time of compilation. The /reference compilation switch will make this reference:

 csc /reference:csAddComp1.dll /out:clAddComp1.exe clAddComp1.cs 

Further reading see the Microsoft .NET pages.

Downloads

Download source - 32 Kb


Comments

  • Hide Set accessors

    Posted by sunitag on 09/29/2005 02:11pm

    How can I set my properties to be read-only outside my namespace.

    Reply
  • Components in C#

    Posted by Legacy on 02/23/2004 12:00am

    Originally posted by: Rajabasha Khan.P

    Excellent Work
    

    Reply
  • Problems when calling from VC++ 6.0 Clinet

    Posted by Legacy on 02/18/2004 12:00am

    Originally posted by: Aryan

    HI All,

    I created a C# component dll and generated typelib. From VB client I am able to access the methods of the C# component.
    If i tried with VC++ client I am getting error message"Members not found". In VC++ When I called the method which takes no parameters and no void return type then it is working fine without any problem. Could u tell me where was the actual problem.


    Thanks in advacne
    Aryan

    Reply
  • Excellent

    Posted by Legacy on 11/27/2003 12:00am

    Originally posted by: vedhamanikandan

    its really good one .... 
    

    Reply
  • Is there any difference between class and component in .NET

    Posted by Legacy on 11/11/2003 12:00am

    Originally posted by: jose

    In VS.NET for the class library project i can add a class or a component class which inherits from the System.Component.ComponentModel then what is the difference between the normal class inherits from system.object and this? What is the advantage i can get if i inherit from System.Component.ComponentModel

    • This example is increadibly misleading...

      Posted by DelboyDee on 11/03/2004 11:19am

      This example is increadibly misleading, it is NOT a component it mearly describes how to create a class dynamic link library. A proper component derives from System.ComponentModel.Component or System.ComponentModel.IComponent which allows you to add the component to a windows form like the Timer or SqlDataAdapter. These components can also be called normally like classes but there is some extra stuff that goes on if you are using it at design time. The only advantage that I know of is that you can set the properties up at design time so it means the IDE auto generates the code for you that sets the properties to the values that you have specified.

      Reply
    Reply
  • Creating components using VS.NET IDE

    Posted by Legacy on 09/27/2003 12:00am

    Originally posted by: Rajmohan

    To develop this in .NET IDE, create a class library project called CompCS. Add a class called csAddComp1.cs and cut and paste the component code. Build solution.

    - Then create a new windows application project and in the main function cut and paste the main class code of the second part. Add a reference to the CompCS.dll from this project. Add using CompCS in the beginning and build solution, execute to see the messages in the console.

    Reply
  • COM dll using VS IDE??

    Posted by Legacy on 06/16/2003 12:00am

    Originally posted by: Aisha Ikram

    You have used command line tools to build a dll. How we can make a COM component dll using Visual Studio .NET IDE, just like we make an ATL component using Visual Studio 6.
    Would you please specify? it will help the programmers who hardly like to work at console.
    Aisha

    Reply
  • Worth noting: "private int i=0,j=0;" in C# class is ok, but will not compile in C++

    Posted by Legacy on 04/22/2003 12:00am

    Originally posted by: roboo - Automated Test Framework for .NET with Many Reusable Testcases and Development Componments

    Can initialize data members in C#, but in C++ you'll get compile error. C# is cool.

    Reply
  • Components in C#

    Posted by Legacy on 11/06/2002 12:00am

    Originally posted by: Sunitha Gona

    Excellent

    Reply
  • COM Client in VC++ 6.0

    Posted by Legacy on 03/06/2002 12:00am

    Originally posted by: Renata

    Hi,
    Can you tell me, how can I create COM object in C#, which can be used from VC++ 6.0. The connection in VC++ 6.0 is still sending this error number "REGDB_E_CLASSNOTREG ($80040154)",which is consequence of COM registration with regasm.exe. Does the COM registration of COM object created in C# dependent on the OS?

    Thank you very much.
    Renata

    Reply
  • Loading, Please Wait ...

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Is your sales and operations planning helping or hurting your bottom line? Here are 5 useful tips from the experts at Quintiq to guide you to a better S&OP strategy.

  • Mobile is introducing sweeping changes throughout your workplace. As a senior stakeholder driving mobile initiatives in your organization, you may be lost in a sea of technologies and claims from vendors promising rapid delivery of applications to your employees, customers, and partners. To help explain some of the topics you will need to be aware of, and to separate the must-haves from the nice-to-haves, this reference guide can help you with applying a mobile strategy in the context of application …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds