Wednesday, June 19, 2013

Various way to set data property of class

There are various ways to set data to class data member . Few of them I have demonstrated here.
Using Property.
Using get and set property you can set value to your data member of class. Here is the example

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data;
using System.Diagnostics;
using System.Data.SqlClient;

namespace BlogProject
{
    class Person
    {
        private string Name;
        private string Surname;

        public string P_Name
        {
            get { return Name; }
            set { Name = value; }
        }
        public string p_Surname
        {
            get { return Surname; }
            set { Surname = value; }
        }
    }


    class Program
    {
        static void Main(string[] args)
         {
             Person p = new Person { P_Name = "Sourav", p_Surname = "Kayal" };
             Console.Write("Name:- "+ p.P_Name + " Surname:- " + p.p_Surname);
             Console.ReadKey();
         }
    }

}





Another way to set data to data member of class is during object creation of that particular class. We can use parameterized constructor to initialize data member. This example shows how to do it.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data;
using System.Diagnostics;
using System.Data.SqlClient;

namespace BlogProject
{
    public class Person
    {
        public string Name;
        public String Surname;

        public Person(String Name, String Surname)
        {
            this.Name = Name;
            this.Surname = Surname;
        }
    }

    class Program
    {
        static void Main(string[] args)
         {
             Person p = new Person("Sourav","Kayal");
             Console.Write("Name:- " + p.Name + "Surname:- " + p.Surname);
             Console.ReadKey();
         }
    }

}


Another popular technique to set data to data member of class is using function. We can use parameterized function to initialize object of class. The code will show how to do it.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data;
using System.Diagnostics;
using System.Data.SqlClient;

namespace BlogProject
{
    public class Person
    {
        public string Name;
        public String Surname;
        public Person SetData(String Name,String Surname)
        {
            Person tmp = new Person();
            tmp.Name = Name;
            tmp.Surname = Surname;
            return tmp;
        }
       
    }

    class Program
    {
        static void Main(string[] args)
         {
             Person p = new Person();
             p= p.SetData("sourav","kayal");
             Console.Write("Sourav:-" + p.Name + " Surname:-"+ p.Surname);
            
             Console.ReadKey();
         }
    }

}


No comments:

Post a Comment