Adsence750x90

Monday, May 18, 2009

What is an Interface? CSharp tutorials and sample code

What is an Interface?
Interface is defined as set of methods properties evnets and fields.
The scope of an inetrface is public.interface contain only signature.(it contain only definition no implementation)
Implementation are done in derived class.Only the interface can support multiple inheritance in C#.



//Example of Interface
interface ITestInterface
{
void TestMethod();
//Declaring Properties in interface
int Count
{
get;
set;
}
//Read only Property
string Item
{
get;
}
//Write only Property
string Name
{
set;
}

}

//interface Implementation are done in derived class.so we are going to inherit ITestInterface in a class named InterfaceImplimentation
public class InterfaceImplimentation:ITestInterface
{
public InterfaceImplimentation()
{
}

#region ITestInterface Members

public void TestMethod()
{
//Write Implimentation
}
private int y;
private string z = string.Empty;
public int Count
{
get
{
return y;
}
set
{
y = value;
}
}
public string Item
{
get
{
return "Item1";
}
}
public string Name
{
set
{
z = value;
}
}

#endregion
}


//implimentation


public class Implementation
{
InterfaceImplimentation if1 = new InterfaceImplimentation();
public void GetItem()
{
if1.TestMethod();
}
}

3 comments:

Ravi said...

Hi Raju,
I have a query:
Show the calendar for 2009 where user can choose the desired month.


I created the drop down list for all the months , but when I select one of the months ,the month doesn't change .I have also added calendar in my page.

I did the following:
.aspx file:

asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
onselectedindexchanged="DropDownList1_SelectedIndexChanged">
asp:ListItem
.aspx.cs file:
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (DropDownList1.SelectedItem.Text == "January")
Calendar1.SelectedDate = DateTime.Parse("January");


}
protected void Calendar1_SelectionChanged(object sender, EventArgs e)
{


}
}

Raju.M said...

use javascript calender contol or jquery calender control. its better to save ur time.

Raju.M said...

its a just example to change one month


protected void DropDownList1_SelectedIndexChanged2(object sender, EventArgs e)
{
DateTime date = DateTime.Now.AddMinutes(1);
Calendar1.TodaysDate = date;
Calendar1.SelectedDate = Calendar1.TodaysDate;
}