Showing posts with label Class. Show all posts
Showing posts with label Class. Show all posts

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.


 public class Bank  
 {  
   public Bank()  
   {  
     miBalance = 5000;  
   }  
   public Bank(int mAmount)  
   {  
     Balance = mAmount;  
   }  
   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;  
   }  
 }  

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

Saturday, January 12, 2008

WPF and Silverlight Creating Class with Constructors

(I have updated this blog on Mar 23, 2010 to showcase Silverlight 4 and Visual Studio 2010.
The same will work with WPF)















Create a new Silverlight 4 Application in Visual Studio 2010.


Creating a Silverlight Application Project creates two projects one a Silverlight Project and another a Web Project used for Hosting Silverlight.

Select Silverlight Project, Add a class by selecting Project, Add class menu option. Give the class a good name say Bank.


So we are going to create a Bank Class with two properties and one method.







In this article we are going to take up the issue of creating a class in C#.

C# has the notion of read write, read only and write only properties. Here we are going to have one read write properties, Bank.

For creating a class there property procedures. For read write property we should have both getter and setter and a private memory variable which actually holds the value.
Getter is used for returning the value of the private memory variable and Setter is used for setting a value to the private memory variable which in effect reflects the value of the property.

The methods are Deposit and Withdraw.

I am also implementing a default and a parameterized constructor.



Here is the code of Bank class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WpFCSClass
{
class Bank
{
public Bank()
{
miBalance = 5000;
}

public Bank(int mAmount)
{
Balance = mAmount;
}
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;
}
}
}

Having created the class we can shift out attention to the presentation layer. The presentation layer can be anything ranging from Windows Forms Application, Web Forms Application, WPF etc. I decided to choose WPF.




















Bank ShalvinBank = new Bank(2000);
private void Window_Loaded(object sender, RoutedEventArgs e)
{
ShowBal();
}
private void ShowBal()
{
lblCurBal.Content = ShalvinBank.Balance.ToString();
txtAmount.Text = "";
txtAmount.Focus();
}

private void btnDeposit_Click(object sender, RoutedEventArgs e)
{
ShalvinBank.Deposit(Int32.Parse(txtAmount.Text));
ShowBal();
}

private void btnWithdraw_Click(object sender, RoutedEventArgs e)
{
ShalvinBank.Withdraw(Int32.Parse(txtAmount.Text));
ShowBal();
}

Happy Programming

shalvin@gmail.com