Thursday, May 15, 2014

Struct


namespace Structures
{

    public struct MyPoint
    {
        //private field
        private double _x;
        private double _y;

        //Constructor
        public MyPoint(double x, double y)
        {
            _x = x;
            _y = y;
        }
        // a struct will always have a default constructor 
        //provided by the compiler regardless whether you define a custom
        //constructor.
        //--------------------
        // the compiler will always provide a struct
        //with a default constructor.
        //you cannot define your own default constructor (for a struct)
        //public MyPoint()
        //{
        //    _x = 2;
        //    _y = 5;
        //}

        //properties
        public double X
        { get { return _x; } }
        public double Y
        { get { return _y; } }

        //method
        //Add a method that computes the distance 
        //between the origin (0, 0) and the point
        public double Distance()
        {
           //double d = Math.Sqrt(Math.Pow(0 - _x, 2) + Math.Pow(0 - _y, 2));
            //double d = Math.Sqrt((0 - _x) * (0 - _x) + (0 - _y) * (0 - _y));
            double d = Math.Sqrt(_x * _x + _y * _y);
            return d;
        }

        //static method (class method)
        //(it is not an instant method)
        //Eg. Distance between two point
        public static double Distance(MyPoint p1, MyPoint p2)
        {
            return Math.Sqrt((p1.X - p2.X) * (p1.X - p2.X) + (p1.Y - p2.Y) * (p1.Y - p2.Y));
        }
    }
}
//========================================
namespace Structures
{
    /*
     Struct
     =======
     * a struct is a lightweight class
     * use the keyword 'struct' to define a structure
     * struct is value type, (class is reference type)
     */
    //A struct has always default constructor

    public partial class Form1 : Form
    {
        List<MyPoint> pointsList = new List<MyPoint>();
        Random rand = new Random();
        public Form1()
        {
            InitializeComponent();
        }

        private void btnCreateSave_Click(object sender, EventArgs e)
        {
            //Create a random MyPoint object
            double x = rand.Next(1, 10) + rand.NextDouble();
            double y = rand.Next(1, 10) + rand.NextDouble();

            //create MyPoint object
            MyPoint p1 = new MyPoint(x, y);
         
            //save
            pointsList.Add(p1);
         
            Display(p1);
        }
        private void Display(MyPoint p)
        {
            //in addition to displaying x, and y
            //display the distance
            richTextBox1.Clear();
            double d = p.Distance();
            richTextBox1.AppendText(String.Format("({0:f2}, {1:f2}, Distance from 0 point: {2:f2})\n", p.X, p.Y, d));
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //preload pointsList with 10 points.
            for (int i = 1; i <= 10; i++)
            {
                double x = rand.Next(1, 10) + rand.NextDouble();
                double y = rand.Next(1, 10) + rand.NextDouble();
                //create MyPoint object
                MyPoint p1 = new MyPoint(x, y);
                //save
                pointsList.Add(p1);
            }
        }

        private void btnDistanceBetweenFirstTwoPoint_Click(object sender, EventArgs e)
        {
            //to access a static methods in a class
            //use the classname (instead of the object name)
            richTextBox1.Clear();
            double d = MyPoint.Distance(pointsList[0], pointsList[1]);

            richTextBox1.Text = String.Format(" P1({0:f}, {1:f}) \n p2({2:f}, {3:f}) \n distance = {4:f}",
                pointsList[0].X, pointsList[0].Y, pointsList[1].X, pointsList[1].Y, d);
        }
    }
}

//Research / read about Static fields and static methods.
//Note: In .Net
//There are structs called .Point .Size .Rectangle

No comments:

Post a Comment