Monday, March 24, 2008

Creating and Consuming Class Library in C#, Object Initializer

Knowledge of working with the class is essential in working with technologies like Web Service, Windows Communication Framework (MCF), Creating ASP .Net Web Controls, Model View Controller (MVC), etc.

I have already demonstrated creating class modules in C# WPF Creating Class with Constructors.
In that blog I creating the class by adding a class to the existing project. The drawback with that model is you can't use the class library outside the project.

Creating Class Library

In Visual Studio Select Class Library Project.



















Visual Studio will create a project with a class called Class1. Better rename the class to Bank.
In this exercise we will be creating a Bank Class with one property called Balance and two methods Deposit and Withdraw.


namespace ShalvinBankLib
{
public class Bank
{
private int miBalance;
public int Balance
{
get
{
return miBalance;
}
set { miBalance = value; }
}
public void Deposit(int Amount)
{
Balance = Balance + Amount;
}
public void Withdraw(int Amount)
{
Balance = Balance - Amount;
}
}
}


Instead of running the project you should build the project by going to Build, Build Solution (F6).

It creates a dll with the name of the project.

C# 3.0 Automatic properties

Writing getter setter and private memory variable for creating a property seem over kill.

private int miBalance;
public int Balance
{
get
{
return miBalance;
}

Instead in with C# 3.0 the programming have to just create a code like the following for creating a property :

public int Balance
{
get;
set;
}
This new language feature incurs no performance set backs because behind the scene it is creating getter setter and private memory variable for you.

Automatic property can be easily created by typing prop tab tab.

Consuming Class Library

Create a Windows Forms Application. You have add a reference to the dll by going to Project, Add Reference.

Select Browse tab in the dialog.


Navigate to the bin directory inside Release folder of the project and select the dll.

Now you can use the dll in your project just like you use the .Net class libraries.

Create a visual interface as shown below.





















using ShalvinBankLib;

Bank ShalvinBank;
private void Form1_Load(object sender, EventArgs e)
{
ShalvinBank = new Bank();
}

private void ShowBalance()
{
lblCurBal.Text = ShalvinBank.Balance();
txtAmount.Text = "";
txtAmount.Focus();
}
private void btnDeposit_Click(object sender, EventArgs e)
{
ShalvinBank.Deposit(Int32.Parse(txtAmount.Text);
ShowBalance();
}
private void btnDeposit_Click(object sender, EventArgs e)
{
ShalvinBank.Withdraw(Int32.Parse(txtAmount.Text);
ShowBalance();
}
}

C# 3.0 Object Initializer
With C# 3.0 it is possible to initialize a class with

Bank ShalvinBank = new Bank { Balance = 5000 };
MessageBox.Show(ShalvinBank.Balance.ToString());

Thus the class can be initialized event without creating a constructor.

Additional Note on Class Libraries
It is very much possible to have forms inside class libraries. This is very useful in ERP type applications where new modules can be easily plugged into existing application simply be setting a reference to the class library and invoking the form from the parent application.


Form Visual Interface Code

private void InitializeComponent()
{
this.btnWithdraw = new System.Windows.Forms.Button();
this.btnDeposit = new System.Windows.Forms.Button();
this.txtAmount = new System.Windows.Forms.TextBox();
this.lblCurBal = new System.Windows.Forms.Label();
this.Label2 = new System.Windows.Forms.Label();
this.Label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// btnWithdraw
//
this.btnWithdraw.Location = new System.Drawing.Point(128, 137);
this.btnWithdraw.Name = "btnWithdraw";
this.btnWithdraw.Size = new System.Drawing.Size(75, 23);
this.btnWithdraw.TabIndex = 11;
this.btnWithdraw.Text = "Withdraw";
this.btnWithdraw.UseVisualStyleBackColor = true;
//
// btnDeposit
//
this.btnDeposit.Location = new System.Drawing.Point(36, 137);
this.btnDeposit.Name = "btnDeposit";
this.btnDeposit.Size = new System.Drawing.Size(75, 23);
this.btnDeposit.TabIndex = 10;
this.btnDeposit.Text = "Deposit";
this.btnDeposit.UseVisualStyleBackColor = true;
//
// txtAmount
//
this.txtAmount.Location = new System.Drawing.Point(116, 81);
this.txtAmount.Name = "txtAmount";
this.txtAmount.Size = new System.Drawing.Size(100, 20);
this.txtAmount.TabIndex = 9;
//
// lblCurBal
//
this.lblCurBal.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.lblCurBal.Location = new System.Drawing.Point(116, 34);
this.lblCurBal.Name = "lblCurBal";
this.lblCurBal.Size = new System.Drawing.Size(87, 23);
this.lblCurBal.TabIndex = 8;
//
// Label2
//
this.Label2.AutoSize = true;
this.Label2.Location = new System.Drawing.Point(30, 44);
this.Label2.Name = "Label2";
this.Label2.Size = new System.Drawing.Size(46, 13);
this.Label2.TabIndex = 7;
this.Label2.Text = "Balance";
//
// Label1
//
this.Label1.AutoSize = true;
this.Label1.Location = new System.Drawing.Point(33, 88);
this.Label1.Name = "Label1";
this.Label1.Size = new System.Drawing.Size(43, 13);
this.Label1.TabIndex = 6;
this.Label1.Text = "Amount";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.btnWithdraw);
this.Controls.Add(this.btnDeposit);
this.Controls.Add(this.txtAmount);
this.Controls.Add(this.lblCurBal);
this.Controls.Add(this.Label2);
this.Controls.Add(this.Label1);
this.Name = "Form1";
this.Text = "Shalvin P D";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
internal System.Windows.Forms.Button btnWithdraw;
internal System.Windows.Forms.Button btnDeposit;
internal System.Windows.Forms.TextBox txtAmount;
internal System.Windows.Forms.Label lblCurBal;
internal System.Windows.Forms.Label Label2;
internal System.Windows.Forms.Label Label1;



Related Blogs
.Net Remoting Walkthrough

No comments:

Post a Comment