Sunday, November 2, 2014

Static Member Of A Class


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



namespace MyLibrary
{
    public class Jar
    {
        // this jar holds marbles
        // backing fields
        private int _currentCount;
        private static int _maxCapacity;

        // properties
        public int CurrentCount
        { get { return _currentCount; } }

        public static int MaxCapacity
        { get { return _maxCapacity; } }

        // constructor
        public Jar(int initialCount)
        {
            if (initialCount > _maxCapacity)
                _currentCount = _maxCapacity;
            else
                _currentCount = initialCount;
        }

        // static constructor ************************************
        // this constructor is called automatically, 
        //the first time any instance of a Jar is created. 
        //It only runs once, and then ALL jar instances have a maxcapacity of 500
        static Jar()
        {
            // only used to initialize static fields. See: 
            ////msdn.microsoft.com/en-us/library/k9x6w0hc.aspx for more information.
            _maxCapacity = 500;
        }
        // methods
        // returns the number of marbles actually added
        public int AddMarbles(int marbles)
        {
            if (_currentCount + marbles <= _maxCapacity)
            {
                _currentCount += marbles;
            }
            else
            {
                marbles = _maxCapacity - _currentCount;
                _currentCount = _maxCapacity;
            }
            return marbles;
        }

        // returns the number of marbles actually taken
        public int RemoveMarbles(int marbles)
        {
            if (_currentCount - marbles < 0)
            {
                marbles -= _currentCount;
                _currentCount = 0;
            }
            else
            {
                _currentCount -= marbles;
            }
            return marbles;
        }
        // add a static method that allows you to compare two jars
        // .NET definition style for comparing two objects
        // return positive (or 1) if 1stObj > 2ndObj
        // return neg (or -1) if 1stObj < 2ndObj
        // return 0 if 1stObj == 2ndObj
        public static int CompareJarCount(Jar j1, Jar j2)
        {
            // Compare the jars based on their currentCount
            if (j1._currentCount > j2._currentCount)
                return 1;
            if (j1._currentCount < j2._currentCount)
                return -1;
            return 0;
        }
    }
}
///////////////////////////////////////////////////////////////////////////////////////////////
using System.Windows.Forms;
using MyLibrary;

namespace Z_StaticMemberOfAClass
{
    public partial class Form1 : Form
    {
        Jar j1, j2;

        public Form1()
        {
            InitializeComponent();
        }

        private void btnCreateJar_Click(object sender, EventArgs e)
        {
            // the instance means object
            // an instance method is a method that can only 
            // be accessed through an instance of the object.
            int initialCount = int.Parse(txtCreateJar.Text);
            j1 = new Jar(initialCount);
            // j1 is an object variable of type Jar
            // j1 is an instance of Class Jar

            // the AddMarbles defined in Jar class is 
            // an instance method, because it requires an 
            // Jar object to be accessed.

            j1.AddMarbles(100);

            // Current count is an instance property also. 
            //It requires a jar object to be seen/have access to.
            int count = j1.CurrentCount;

            // in addition to instance properties and methods
            // a class can define static fields, perperties, and methods.

            // Another name for static properties or methods is class properties or
            // class methods. They can be accessed through the class itself, only!

            int maxCap = Jar.MaxCapacity;

            j2 = new Jar(initialCount);

            // Add Marbles to each jar
            // added to j1 already above ^
            j2.AddMarbles(200);
            // compare the two jars
            switch (Jar.CompareJarCount(j1, j2))
            {
                case 0:
                    MessageBox.Show("Both Jars contain the same number of marbles");
                    break;
                case 1:
                    MessageBox.Show("Jar 1 has more marbles than Jar 2");
                    break;
                case -1:
                    MessageBox.Show("Jar 2 has more marbles than Jar 1");
                    break;
                default:
                    MessageBox.Show("Something unexpected happened");
                    break;
            }
        }
    }
}

No comments:

Post a Comment