Showing posts with label Example. Show all posts
Showing posts with label Example. Show all posts

Monday, August 26, 2013

Implementing Factory design pattern in asp.net using c# | Design Patterns | Example Factory design pattern

hi in this post i will show how to implement the Factory design pattern in asp.net

In Factory design pattern we cannot directly create the object of the class we need to send our requirement to the factory class. This factory class will provide us the required object.

it helps us to create object without exposing the instantiation logic to client.

Example :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ConsoleApplication1
{
    interface color
    {
        string GetCarColor();
    }

    class myFactory
    {
        public color GetObj(int type)
        {
            color interfaceObj = null;
            switch (type)
            {
                case 1:
                    interfaceObj = new car1();
                    break;
                case 2:
                    interfaceObj = new car2();
                    break;
                case 3:
                    interfaceObj = new car3();
                    break;
            }
            return interfaceObj;
        }
    }

    class car1 : color
    {
        public string GetCarColor()
        {
            return "Red color car";
        }

    }
    class car2 : color
    {
        public string GetCarColor()
        {
            return "Blue color car";
        }
    }
    class car3 : color
    {
        public string GetCarColor()
        {
            return "White color car";
        }
    }
}

/////Main Class

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            myFactory obj = new myFactory();
            color c = obj.GetObj(2);
            string s = c.GetCarColor();
            Console.WriteLine(s);
            Console.ReadLine();

        }
    }
}

Thursday, July 11, 2013

WCF Method Overloading Example in c#.net | Implementing method overloading in WCF

hi in post i will show how to implement method overloading in WCF using c#.net.

Example :

Take a new WCF Service Application Project.

1. IService1.cs --> Interface code

 [ServiceContract]
    public interface IService1
    {
        [OperationContract(Name="GetDataValue1")] //Method overloading
        string GetData(int value);

        [OperationContract(Name = "GetDataValue2")] //Method overloading
        string GetData(string value);
    }

2. Service.svc.cs 

    public class Service1 : IService1
    {
        public string GetData(int value)
        {
            return string.Format("You entered: {0}", value);
        }

        public string GetData(string value)
        {
            return string.Format("You entered: {0}", value);
        }
       
    }

3. Result :




Monday, June 24, 2013

bind data to Repeater in asp.net | Repeater Example

hi in this post i will demonstrate on how to bind data to a Repeater in asp.net.

Aspx code :

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Repeater ID="Repeater1" Visible="true" runat="server">
            <HeaderTemplate>
             <table border="1" width="30%">
                    <tr>
                        <th>
                            Name
                        </th>
                        <th>
                            Salary
                        </th>
                    </tr>
            </HeaderTemplate>
            <ItemTemplate>
                <tr>
                    <td>
                        <%#DataBinder.Eval(Container.DataItem, "Name")%>
                    </td>
                    <td>
                        <%#DataBinder.Eval(Container.DataItem, "Salary")%>
                    </td>
                </tr>
            </ItemTemplate>
          
            <FooterTemplate>
                </table>
            </FooterTemplate>
        </asp:Repeater>
    </div>
    </form>
</body>
</html>

Codebehind:

 public void BindRepeater()
    {

        string conStr = ConfigurationManager.ConnectionStrings["NORTHWNDConnectionString"].ConnectionString.ToString();
        SqlConnection s1 = new SqlConnection(conStr);
        s1.Open();

        string queryString = "select empname as Name,salary from employee";
        SqlDataAdapter adapter = new SqlDataAdapter(queryString, s1);

        DataSet employee = new DataSet();
        adapter.Fill(employee, "employee");

        Repeater1.DataSource = employee.Tables[0];
        Repeater1.DataBind();

        s1.Close();

    }

Result :


Sunday, June 16, 2013

Struct and Class Example | Difference Between Struct and Class

Structs and Class:

struct class
1 It is value type It is Reference Type
2 Default access specifier is Public Default access specifier is Private
3 It only has Constructor It has both Constructor and Destructor
4 Struct has values residing on Stack  Class have Object values residing on Heap and it has references to the objects on stack
5 do not support Inheritance It Supports Inheritance

Example:

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

namespace ConsoleApplication3
{
    struct emp
    {
        public int empid;
        public string empname;
    }

    class dept
    {
        public int deptid;
        public string deptname;
    }

    class Program
    {
        static void Main(string[] args)
        {
            emp e = new emp();
            emp e2 = e;

            e.empid = 1;
            e.empname = "chandan";

            e2.empid = 2;
            e2.empname = "Rahul";

            Console.WriteLine("ID:{0} and Name:{1}", e.empid, e.empname);
            Console.WriteLine("ID2:{0} and Name2:{1}", e2.empid, e2.empname);

            dept d = new dept();
            dept d2 = d;

            d.deptid = 100;
            d.deptname = "Accounts";

            d2.deptid = 101;
            d2.deptname = "IT";

            Console.WriteLine("ID:{0} and Name:{1}", d.deptid, d.deptname);
            Console.WriteLine("ID2:{0} and Name2:{1}", d2.deptid, d2.deptname);

            Console.ReadLine();
        }
    }
}

Result :

class object referring to the same values whereas struct has new values assigned