Zebra0.com

csharp objects


Text of video

Code for the class:

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

namespace OOPS
{
    class Fraction
    {
        // Member attibutes or proerties
        private int Numerator;
        private int Denominator;

        // Constructor with no parameters
        // Example Fraction myFrac=new Fraction();
        public Fraction()
        {
            Numerator = 0;
            Denominator = 1;
        }

        // Constructor with two integer parameters
        // Example Fraction half=new Fraction(1,2);
        public Fraction(int numerator, int denominator)
        {
            // Any numerator is ok.
            Numerator = numerator;
            // Denominator can never be 0!
            if (denominator != 0) Denominator = denominator;
            else Denominator = 1;
        }

        // Override the ToString method: example 1/2
        public override String ToString()
        {
            return "" + Numerator + "/" + Denominator;
        }

    }
}

Code in form load to test:

private void Form1_Load(object sender, EventArgs e)
{
   Fraction myFrac = new Fraction(1,0);
   Text = myFrac.ToString();
}

End of lesson, Next lesson: Zebra Notes: Open, Save, Save As, and Format with a Rich Text Box