compcode

This is an old revision of the document!


Sample of a Bean

Here is a sample code of a component in C# (save this to a file called Your_CSharpBean_Name.cs) :

using WComp.Beans;      // Contains the definition of the [Bean] attribute
using System.Threading; // For the thread demo purposes
 
namespace Your_Namespace_Name {
 
    [Bean]
    public class Your_CSharpBean_Name {
 
        public Your_CSharpBean_Name() {
            // Put here your init instructions
            // Example of a loop that call the output
            // every second.
            new Thread(new ThreadStart(ThreadLoopMethod)).Start();
        }
 
         // Loop sample
         public void ThreadLoopMethod() {
             while(Thread.CurrentThread.IsAlive) {
                 Thread.Sleep(1000);
                 double result;
                 // Check if the output is connected
                 if(Output_Sample != null)
                     // call the connected methods sequentially
                     result = Output_Sample(123);
                // and so on...
             }
        }
 
        // --- Start: Input port sample ---
        // an input port is a method (below)
        public int Input_Sample(string param_name) {
            string pn = param_name; // You may use the parameters
            return 0; // You may return anything
        }
        // --- End: Input port sample ---
 
        // --- Start: Output port sample ---
        public delegate double Output_Sample_Signature(int param_name);
        // This is the signature of the output method
        public event Output_Sample_Signature Output_Sample;
        // The output port is the event, named here Output_Sample
        // --- End: Output port sample ---
    }
}

Here is a sample of the command line compilation:

csc.exe /target:library /r:Beans.dll Sample_Component.cs

You will find Beans.dll in the SharpWComp distrib.

  • compcode.1294238583.txt.gz
  • Last modified: 2011/01/05 15:43
  • by hourdin