C#

To Bottom

// C#

// Declarations

To Top

// Input

To Top

// Output

To Top

// Casting

Impicit Casting: byte->short->int->long->decimal

Explicit Casting:

To Top

// DateTime

To Top

// Case Switch

   
    int caseSwitch = 1;
    switch (caseSwitch)
    {
        case 1:
            Console.WriteLine("Case 1");
            break;
        case 2:
            Console.WriteLine("Case 2");
            break;
        default:
            Console.WriteLine("Default case");
            break;
    }
    

To Top

// More

To Top

// MyMethods


    GetInput(out number1, out number2, out operation);
	
    /**
     * Example of out-ref
     */
    public void GetInput(out int number1, out int number2, out string tmpOperator)
    {
        number1 = Convert.ToInt16(txtNumber1.Text);
        number2 = Convert.ToInt16(txtNumber2.Text);
        tmpOperator = txtOperation.Text;
    }
    /**
     * Validate the input
     */
    private bool IsValid()
    {
        bool valid = false;
		
        //if (CheckForString(txtScore) && IsNumeric(txtScore))
        if (txtScore.Text != "" && IsNumeric(txtScore))
        {
            valid = true;
        }
        return valid;
    }
		
    /**
     * Check that TextBox is not empty
     */
    private bool CheckForString(TextBox tBox)
    {
        bool badString = false;
        if (tBox.Text.Length > 0)
        {
            badString = true;
        }
        return badString;
    }
    /**
     * Check that TextBox contains a number and is in range
     */
    private bool IsNumeric(TextBox tBox)
    {
        bool valid = false;
        try
        {
            if (Convert.ToInt16(tBox.Text) >= 0 && Convert.ToInt16(tBox.Text) <= 100)
            {
                valid = true;
            }
            else
            {
                // OverflowException the number was out of range
                MessageBox.Show("Valid test scores range\nfrom 0 to 100.");
            }
        }
        catch(FormatException)
        {
            // it was not a number
            valid = false;
            MessageBox.Show("Enter a number\nfrom 0 to 100.");
        };
        return valid;
    }
		
    /**
     * Exit button - close the form
     */
    private void btnExit_Click(object sender, EventArgs e)
    {
        this.Close();
    }
    

To Top

// Lessons


    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    /*
     * Pogram3 - Score Calculator
     * Description:
     * Author Craig Pounds
     * Version Nov 13, 2014
     */
    namespace ScoreCalculator
    {
        public partial class frmScoreCalculator : Form
        {
            public frmScoreCalculator()
            {
                InitializeComponent();
            }
            //  decalare variables
            int[] scores = new int[5];
            int scoreTotal = 0;
            int scoreCount = 0;
            int average;
            /**
             * Add button - for valid data, calls clearScores if 
             * scoreCount > the size of the Array scores, calls
             * CalculateScores() and FormOutput.
             * Returns the focus to txtScore and selects its text.
             */
            private void btnAdd_Click(object sender, EventArgs e)
            {
                if (IsValid())
                { 
                    if (scoreCount > scores.GetUpperBound(0))
                    {
                        ClearScores();
                    }
                    CalculateScores();
                    FormOutput();
                    txtScore.Focus();
                    txtScore.SelectAll();
                }
            }
            /**
             * Display scores button - builds a string from
             * the current values in the scores array and
             * then prints it in a MessageBox "Scores"
             */
            private void btnDisplay_Click(object sender, EventArgs e)
            {
                string scoreMessage = "";
                for (int i = 0; i < scoreCount; i++)
                {
                    scoreMessage += scores[i] + "\n";
                }
                MessageBox.Show(scoreMessage, "Scores");
            }
            /**
             * Clear scores button - call clearScores(),
             * clear and return focus to input textbox.
             */
            private void btnClear_Click(object sender, EventArgs e)
            {
                ClearScores();
                txtScore.Text = "";
                txtScore.Focus();
            }
            /**
             * Clear the textboxes and global variables
             * for scoreTotal scoreCount and scoreAverage.
             */
            private void ClearScores()
            {
                txtTotal.Text = "";
                txtCount.Text = "";
                txtAverage.Text = "";
                scoreTotal = 0;
                scoreCount = 0;
                average = 0;
            }
            /**
             * Exit button - close the form
             */
            private void btnExit_Click(object sender, EventArgs e)
            {
                this.Close();
            }
            /**
             * Calculate the average score, scoreTotal and
             * update the scoreCount
             */
            private void CalculateScores()
            {
                int total = 0;
                scores[scoreCount] = Convert.ToInt16(txtScore.Text);
                for (int i = 0; i <= scoreCount; i++)
                {
                    total += scores[i];
                }
                average = total / (scoreCount + 1);
                scoreTotal = total;
                scoreCount++;
            }
            /**
             * Output scoreTotal, scoreCount and
             * average to the form
             */
            private void FormOutput()
            {
                txtTotal.Text = Convert.ToString(scoreTotal);
                txtCount.Text = Convert.ToString(scoreCount);
                txtAverage.Text = Convert.ToString(average);
            }
            /**
             * Validate the input
             */
            private bool IsValid()
            {
                bool valid = false;
                if (CheckForString(txtScore) && IsNumeric(txtScore))
                {
                    valid = true;
                }
                return valid;
            }
            /**
             * Check that TextBox is not empty
             */
            private bool CheckForString(TextBox tBox)
            {
                bool badString = false;
                if (tBox.Text.Length > 0)
                {
                    badString = true;
                }
                return badString;
            }
            /**
             * Check that TextBox contains a number and is in range
             */
            private bool IsNumeric(TextBox tBox)
            {
                bool valid = false;
                try
                {
                    if (Convert.ToInt16(tBox.Text) >= 0 && Convert.ToInt16(tBox.Text) <= 100)
                    {
                        valid = true;
                    }
                    else
                    {
                        // OverflowException the number was out of range
                        MessageBox.Show("Valid test scores range\nfrom 0 to 100.");
                    }
                }
                catch (FormatException)
                {
                    // it was not a number
                    valid = false;
                    MessageBox.Show("Enter a number\nfrom 0 to 100.");
                };
                return valid;
            }
        }
    }
    

To Top