Friday, November 8, 2013

Inside Interface:-

Good example for this would be like a computer hardware engineer who does not have to know how a The circuit board is designed in the hard disk or the mother board as long as he Knows that if he replaced it with a new one with the correct specification, it Would work perfectly.
This is Really a robust feature in software development.
Same you can write an interface and send to the developer To implement all this.

·         Setup strict restriction on the application.
·         Same as inheritance and encapsulation can be done with This.
·         Mostly like pure abstract class which don’t have Implementation.
·         A class can have multiple interface but single base class
·         Loosely coupled application, you know we can create Interfaces and change in the application, the interface can be used in another project Too.
·         In application architecture, roll can be defined in the Developer, work can start parallel like I have created an interface for my web Application, designer can start work on this and same time developer can start Wring the base logics.
·         Ease to implement new features and reusability.

Defining And interface in an application and how we will use this.
1.       Add a class library project and give it the name as “SearchBook”
2.       Interface name should be like ISearchBook, The prefix should start with “I”
This will looks like below
  interface ISearchBook
    {
        List<string> GetSearchResult(String searchPharse);   
    }
Below is the sample code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SampleWindow
{
    public partial class Form1 : Form ,ISearchBook
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            System.Console.Write("Here Interface invoked");    
            InterfaceGetName("test");
            List<string> Result = GetSearchResult("1");
           
        }
 
        public void InterfaceGetName(string Name)
        {
            System.Console.Write(Name);
        }

        public List<string> GetSearchResult(String searchPharse)
        {
           
            List<string> Books = new List<string> { "1", "2", "3", "4" };
            List<string> Result= new List<string>();

            Result = (from A in Books where A.StartsWith(searchPharse) select A).ToList();
           
            return Result;
        }
    }

    interface ISearchBook
    {
        List<string> GetSearchResult(String searchPharse);   
    }
  
}

Implementing interfaces details with practical examples
1.       Frist I created new project in VS 2010. Empty project, Added new page(home.aspx), added new link lnkInterfaces when running the project it gives me error.
handle clause requires a withevents variable
Slution: declared the withevent for linkbuttion  where class strat(Inside class)
 Private WithEvents lnkInterfaces As LinkButton
2.       Added New window application, then added new interface on this through new item
namespace SampleWindow
{
    interface Interface1
    {
        int test;
    }
}
This was giving some exception that
Error       1              Interfaces cannot contain fields
Open Question :-  Why interfaces cannot contain fields ??
3.       We can create a property over there
int A { get; set; }
4.       We cannot add a private access modifier this will give below exception

The modifier 'private' is not valid for this item              

Sample interface
  interface Interface1
    {
       
        void InterfaceGetName(string Name);
     

    }

Used in class
    public partial class Form1 : Form ,Interface1
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            System.Console.Write("Here Interface invoked");
    
            InterfaceGetName("test");
           
        }
 
        public void InterfaceGetName(string Name)
        {
            System.Console.Write(Name);
        }
    }

Point : - if we inherit the interface in a class then all the function need to be implemented in the derived class.

No comments:

Post a Comment