Tuesday, 6 May 2014

Property in C#


/*
 * Properties are not limited to accessing private data—properties also can be used to
calculate values associated with an object. One example of this would be a student
object with a property representing the student’s GPA (called GPA). Programmers can
either provide code that calculates the student’s GPA in the get accessor for this property,
or they can simply return a private variable containing the GPA, called gpa. (The value
in this variable will need to be calculated in some other way, such as using a CalculateGPA
method.) The programmer can use either technique, but we recommend using a
property that calculates the GPA. Remember that client code should not be required to tell
 * */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Exercise_Constructor_Property
{
    class program
    {
        
        static void Main(string[] args)
        {
            student hamza = new student(); //here we are calling perameter less constructor


            //Going to call OverLoaded Constructor or Perameterized Constructor

        //    public string name;
        //public string cell_number;
        //public string fathers_name;
        //public string gerend;
        //public string school;
        //public string caste;


        //private string cnic;
        //private string registration;
        //private float Gpa;
        //private string semester;
        //private string bloud_group;

            student Tahir = new student("Tahir Ali"," 09333-5671122","Hamza","Shemale ","Lgs"
                ,"Rao","32209-8745321-9","098-A-8",2,"4rth","A+");

            

            Tahir.Profile(); //here we are calling thre Profile method of class student


            //Here we are going to assign private instance variables values
            Tahir.Physics = 90;
            Tahir.Bio = 60;
            Tahir.Chemistry = 80;
            Tahir.Urdu = 95;
            Tahir.English = 91;

            //Calling Gpa Method
            Tahir.Gpa_calculator();

            Console.ReadKey();



        }
    }
}


......................................................................................................................................................................................................................................................................................................................................


//This is Student Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Exercise_Constructor_Property
{
    class student
    {
        //Declearation of instance values 
        public string name;
        public string cell_number;
        public string fathers_name;
        public string gerend;
        public string school;
        public string caste;


        private string cnic;
        private string registration;
        private double Gpa;
        private string semester;
        private string bloud_group;

        //obtain marks in different subjects

        private int physics;
        private int bio;
        private int chemistry;
        private int urdu;
        private int english;


        //with out perametes Constructor of Class
        public student()
        {
            set_student_values("","","","","","","","",0,"","");
            Console.WriteLine("I am in Papameterless Constructor:");
        }


        //Peremeterized or Overloded Constructor

        public student(string a, string b, string c, string d, string e,
            string f, string g, string h, float i, string j, string k)
        {

            set_student_values(a, b, c, d, e, f, g, h, i, j, k);
            MessageBox.Show("It is Running Perameterized Constructor :)");
        }

        /*

        public student()
        {
            name = "";
            cell_number="";
            fathers_name = "";
            gerend = "";
            school = "";
            caste = "";
            cnic = "";
            registration = "";
            Gpa = "";
            semester = "";
            bloud_group = "";
           // physics = "" ;
            //bio = "";
            //Constructor cannot assign values to private instance values
        }
         */

        public void  set_student_values(string a,string b,string c,string d
            ,string e,string f,string g,string h,float i,string j,string k)
        {
            name = a;
            cell_number = b;
            fathers_name = c;
            gerend = d;
            school = e;
            caste = f;
            cnic = g;
            registration = h;
            Gpa = i;
            semester = j;
            bloud_group = k;
            // physics = "" ;
            //bio = "";
            //Constructor cannot assign values to private instance values
        }



        //Properties to initialize private instance values
        public int Physics  //Here we change the name of property so that
            //make different from instance variable otherwise find error of 
            //this variable is defined more than once
        {
            get
            {
                return physics;
            }
            set
            {
                physics =(( value>=50 && value <100) ? value : 0);
            }
        }


        public int Bio
        {
            get
            {
                return bio;
            }
            set
            {
                bio = ((value >= 50 && value < 100) ? value : 0); ;
            }
        }
        public int Chemistry
        {
            get
            {
                return chemistry;
            }
            set
            {
                chemistry = ((value >= 50 && value < 100) ? value : 0); 
            }
        }
        public int Urdu
        {
            get
            {
                return urdu;
            }
            set
            {
                urdu = ((value >= 50 && value < 100) ? value : 0); 
            }
        }
        public int English
        {
            get
            {
                return english;
            }
            set
            {
                english = ((value >= 50 && value < 100) ? value : 0); 
            }
        }




        //A methot which will use property too set ang get private values

        public void priv_value_seter(int a,int b,int c,int d,int e)
        {
           //we can't declear a Property in a method tooo initalize a private value 

            //but we can do this
        }


         //This method will calculate the Gpa of Student

        public void Gpa_calculator()
        {
            int sum;
            //int x = 500;
            float average;
            double average_dbl;
            sum = (physics + bio + chemistry + urdu + english);
            MessageBox.Show("Sum=\t" + sum + "\n");
            average = sum / 500; //int devided by int gives int value zeroo
            average_dbl = sum / 500.0; //int devided by float value gives us double value 
            MessageBox.Show("Average=\t" + average + "  dble =   "+ average_dbl +"\n");
            //return average;
            Gpa = average_dbl * 4;
            MessageBox.Show("Gpa of Tahir is:\t" + Gpa+ "\n" + "Marks of Physics:\t" + physics+ "\n");
//return Gpa;

        }


        //This Method will Shows the Public Profile of Student

        public void Profile()
        {
          MessageBox.Show(" Name:\t" + name + "\n"  + "Father's Name:\t" + fathers_name + "\n" + "CNIC number:\t" + cnic+ "\n"
                + "Cell Phone Number:\t" + cell_number + "\n" + "Gerend:\t" + gerend + "\n" + "School:\t" + school + "\n"
                + "Caste:\t" + caste + "\n" + "Bloud Group:\t" + bloud_group + "\n" + "Registration Number:\t" + registration+ "\n"
                + "Semester:\t" + semester + "\n");
        }
        



    }
}

No comments:

Post a Comment