/* */

Constructor Example

constructor is a special method of class which has name as class name . Coming to the point if you look at the below example. We Will have a method inside a class with same name as class name.using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MyFirstHelloWorld.Models
{
    public class Customer
    {
        public int ID { get; set; }
        public string customerCode { get; set; }
        public double amount { get; set; }

        public Customer(int Id, string CustomerID, double Amount)
        {
            ID = Id;
            customerCode = CustomerID;
            amount = Amount;
        }

        public Customer()
        {


        }

    }
}


Now the Question is y i implemented this ..What is the need of this extra method.

If you check the below method . It will through build error due to   Customer cst = new Customer(); if we remove Public Customer () from the above method. So thats one of the reason i have implemented constuctor on the same.
public ActionResult DisplayCustomer()
        {
            Customer cst = new Customer();
            cst.ID = Convert.ToInt32(Request.Form["customerid"]);
            cst.customerCode = Request.Form["customerCode"];
            cst.amount = Convert.ToDouble(Request.Form["Amount"]);

            return View(cst);
            //return View();
        }





Previous
Next Post »
Thanks for your comment