Author Archives: sparky42

A thought

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Exam2_sampleExam1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void buttonClose_Click(object sender, EventArgs e)
        {
            Application.Exit();

        }

        // Decides which form to display depending on choice
        private void buttonView_Click(object sender, EventArgs e)
        {
            try
            {

                if (radioButtonBranch.Checked == true)
                {
                    Form2 form2 = new Form2();
                    form2.ShowDialog();

                }
                else if (radioButtonProfitability.Checked == true)
                {
                    Form3 form3 = new Form3();
                    form3.ShowDialog();

                }
                else
                {
                    throw new
                        InvalidOperationException
                        (“You must select a radio button option.”);
                }
            }
            catch (InvalidOperationException ioEx)
            {
                MessageBox.Show(ioEx.Message, “Error”);
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message, “Error”);
            }
        }
    } // End class
} // End namespace

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Exam2_sampleExam1
{
    public partial class Form2 : Form
    {
        // Declare a 2D Array to populate a table with Branch Details
        private string[,] branchDetails = new string[5, 5];

        private string[] branchNames =
        { “Cork”, “Dublin”, “Galway”, “Limerick”, “Waterford” };

        private string[] branchColumns = { “Sort Code”, “Address”,
                                             “Manager”, “Telephone No.”, “Fax No.” };

        public Form2()
        {
            InitializeComponent();
        }

        private void buttonClose_Click(object sender, EventArgs e)
        {
            Close();

        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            try
            {
                // declare variables for user input
                string sortCode = textBoxSortCode.Text;
                string address = textBoxBranchAddress.Text;
                string manager = textBoxManager.Text;
                string telephoneNo = textBoxTelephoneNo.Text;
                string faxNo = textBoxFaxNo.Text;

                //
                int branchNameIndex = listBoxBranch.SelectedIndex;

                // assign variables to the Indexes of listBoxBranch which will be
                // used to populate a table in the Details Textbox (i.e. textBoxDetails)
                branchDetails[branchNameIndex, 0] = sortCode;
                branchDetails[branchNameIndex, 1] = address;
                branchDetails[branchNameIndex, 2] = manager;
                branchDetails[branchNameIndex, 3] = telephoneNo;
                branchDetails[branchNameIndex, 4] = faxNo;
            }
            catch (FormatException fEx)
            {
                MessageBox.Show(fEx.Message, “Error”);
            }
            catch (NullReferenceException nEx)
            {
                MessageBox.Show(nEx.Message, “Error”);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, “Error”);
            } // End Try Catch

        }

        private void buttonDisplay_Click(object sender, EventArgs e)
        {
            textBoxDetails.Text = “”;
            textBoxDetails.Text = “\t\t”;

            int i =0;
            foreach (string column in branchColumns)
            {
                textBoxDetails.Text += column;
                if (i == 0 || i == 1 || i == 2)
                {
                    textBoxDetails.Text += “\t\t”;
                }
                else
                {
                    textBoxDetails.Text += “\t”;
                }
                i++;

            }

            textBoxDetails.Text += “\r\n”;

            for (int row = 0; row < 5; row++)
            {
                textBoxDetails.Text += branchNames[row];

                for (int column = 0; column < 5; column++)
                {
                    
                   textBoxDetails.Text += “\t\t”;
                    
                    textBoxDetails.Text += branchDetails[row, column];
                } // end inner ‘for’ Loop

                textBoxDetails.Text += “\r\n”;

            } // end outer ‘for’ Loop

        }

    } // End class
} // End namespace

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Exam2_sampleExam1
{
    public partial class Form3 : Form
    {
        private string[,] profitabilityDetails = new string[5,5];

        private string[] branchNames = { “Cork”, “Dublin”, “Galway”, “Limerick”, “Waterford” };

        public Form3()
        {
            InitializeComponent();
        }

        private void buttonClose_Click(object sender, EventArgs e)
        {
            Close();

        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            try
            {
                // parsed out
                int parsedOutstandingLoans = int.Parse(textBoxOutstandingLoans.Text);
                int parsedUnperformingLoans = int.Parse(textBoxUnperformingLoans.Text);
                decimal parsedRevenue = decimal.Parse(textBoxRevenue.Text);
                decimal parsedProfit = decimal.Parse(textBoxProfitability.Text);
                int parsedNoCustomers = int.Parse(textBoxNoCustomers.Text);

                //
                string outstandingLoans = textBoxOutstandingLoans.Text;
                string unperformingLoans = textBoxUnperformingLoans.Text;
                string revenue = textBoxRevenue.Text;
                string profit = textBoxProfitability.Text;
                string noCustomers = textBoxNoCustomers.Text;
                int selectedBranch = listBoxBranchName.SelectedIndex;

                // assigns variables to values in listBox
                profitabilityDetails[selectedBranch, 0] = outstandingLoans;
                profitabilityDetails[selectedBranch, 1] = unperformingLoans;
                profitabilityDetails[selectedBranch, 2] = revenue;
                profitabilityDetails[selectedBranch, 3] = profit;
                profitabilityDetails[selectedBranch, 4] = noCustomers;
            }
            catch (FormatException fEx)
            {
                MessageBox.Show(fEx.Message, “Error”);
            }
            catch (NullReferenceException nrEx)
            {
                MessageBox.Show(nrEx.Message, “Error”);
            }

            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message, “Error”);
            }
        }

        private void buttonDisplay_Click(object sender, EventArgs e)
        {
            // Displays user input in listView for each Branch’s profitability

            //Clears the ListView of any previous data
            listViewProfitability.Items.Clear();

            // **MORE EFFICIENT WAY OF DOING IT** <== as it’s done using a ListView
            for (int row = 0; row < 5; row++)
            {
                ListViewItem item = new ListViewItem();
                item = listViewProfitability.Items.Add(branchNames[row]);

                for (int column = 0; column < 5; column++)
                {
                    item.SubItems.Add(profitabilityDetails[row, column]);
                }

            }

        }
    } // End class
} // End namespace

Try it yourself

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO; //needed for file save

namespace Sample3
{
    public partial class Form1 : Form
    {// to create the Array but without defining the number of inputs
        private string[,] contractorSectors;

        private string[,] contractorProfessions;

        private string[] sectors = { “Pharmaceuticals”, “Banking”, “Software” };

        private string[] professions = { “Accounting”, “Marketing”, “HR”, “IT” };

        private int noContractors = 0;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            comboBoxProfession.Items.AddRange
                (new string[] { “Accounting”, “Marketing”, “HR”, “IT” });
            comboBoxSector.Items.AddRange
                (new string[] { “Pharmaceuticals”, “Banking”, “Software” });
        }

        //exit the app code
        private void buttonExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        //to clear all inputted data.
        private void buttonClear_Click(object sender, EventArgs e)
        {
            textBoxName.Text = “”;
            comboBoxSector.SelectedIndex = -1;
            comboBoxProfession.SelectedIndex = -1;
            listViewProfession.Items.Clear();
            listViewSector.Items.Clear();
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            try
            {
                string contractorName = textBoxName.Text;

                if (contractorName == “”)
                {
                    throw new InvalidOperationException
                        (“The contractor name cannot be empty”);
                }

                int selectedSector = comboBoxSector.SelectedIndex;
                int selectedProfession = comboBoxProfession.SelectedIndex;

                noContractors++;

                string[,] tmpContractorSectors = contractorSectors;//tmp = temporary arrays
                string[,] tmpContractorProfession = contractorProfessions; //ditto

                contractorSectors = new string[3, noContractors];
                contractorProfessions = new string[4, noContractors];

                if (tmpContractorSectors != null)
                {
                    for (int row = 0; row < 3; row++)
                    {
                        for (int column = 0; column < (noContractors – 1); column++)
                        {
                            if (tmpContractorSectors[row, column] == null)
                            {
                                continue;
                            }

                            contractorSectors[row, column] = //puts the temp entries into the arrary
                                tmpContractorSectors[row, column];
                        }

                    }
                }

                if (tmpContractorProfession != null)
                {
                    for (int row = 0; row < 4; row++)
                    {
                        for (int column = 0; column < (noContractors – 1); column++)
                        {
                            if (tmpContractorProfession[row, column] == null)
                            {
                                continue;
                            }

                            contractorProfessions[row, column] =
                                tmpContractorProfession[row, column];

                        }
                    }
                }

                contractorSectors[selectedSector, noContractors – 1] = contractorName; //nocontractors -1 means going to the last entry
                contractorProfessions[selectedProfession, noContractors – 1] = contractorName;
            }

            catch (InvalidOperationException ioE)
            {
                MessageBox.Show(ioE.Message, “Error”);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, “Error”);
            }
        }

        private void buttonDisplay_Click(object sender, EventArgs e)
        {
            listViewProfession.Items.Clear();
            listViewSector.Items.Clear();

            for (int row = 0; row < 3; row++)
            {
                for (int column = 0; column < noContractors; column++)
                {
                    if (contractorSectors[row, column] == “” ||
                        contractorSectors[row, column] == null)
                    {
                        continue;
                    }

                    string sector = “”;
                    if (row == 0)
                    {
                        sector = “Pharmaceuticals”;
                    }
                    else if (row == 1)
                    {
                        sector = “Banking”;
                    }
                    else if (row == 2)
                    {
                        sector = “Software”;
                    }

                    ListViewItem item = new ListViewItem();
                    item = listViewSector.Items.Add(sector);
                    item.SubItems.Add(contractorSectors[row, column]);

                }
            }

            for (int row = 0; row < 4; row++)
            {
                for (int column = 0; column < noContractors; column++)
                {
                    if (contractorProfessions[row, column] == “” || contractorProfessions[row, column] == null)
                    {
                        continue;
                    }

                    string profession = “”;
                    if (row == 0)
                    {
                        profession = “Accounting”;
                    }
                    else if (row == 1)
                    {
                        profession = “Marketing”;
                    }
                    else if (row == 2)
                    {
                        profession = “HR”;
                    }
                    else if (row == 3)
                    {
                        profession = “IT”;
                    }

                    ListViewItem item = new ListViewItem();
                    item = listViewProfession.Items.Add(profession);
                    item.SubItems.Add(contractorProfessions[row, column]);
                }
            }
        }

        private void buttonFileSave_Click(object sender, EventArgs e)
        {
            TextWriter writer = null;
            try
            {
                FileStream stream = File.Open(“E:\\contractors.dat”,//file name I’ve assigned for this solution
                    FileMode.OpenOrCreate);//code used to access or create the file that will be writen to.
                writer = new StreamWriter(stream);

                for (int row = 0; row< 3; row++)
                {
                    for (int column = 0; column < noContractors; column++)
                    {
                        if (contractorSectors[row, column] == “”||
                            contractorSectors[row, column] == null)

                        {
                            continue;
                        }

                        string sector = “”;
                        if (row==0)
                        {
                            sector = “Pharmaceuticals”;
                        }
                        else if (row==1)
                        {
                            sector= “Banking”;
                        }
                        else if (row==2)
                        {
                            sector = “Software”;
                        }

                    writer.Write(sector);// code used to write to file
                    writer.Write(” “);
                    writer.Write(contractorSectors[row,column]);
                    writer.WriteLine();
                    writer.Flush();//flush is the command to transfer the data to the file
                    }

                }

                for (int row = 0; row<4; row++)
                {
                    for (int column = 0; column<noContractors; column++)
                    {
                        if (contractorProfessions[row,column]== “”
                            || contractorProfessions[row,column] == null)
                        {
                            continue;
                        }

                        string profession = “”;
                        if (row==0)
                        {
                            profession = “Accounting”;
                        }
                        else if (row ==1)
                        {
                            profession = “Marketing”;
                        }
                        else if (row ==2)
                        {
                            profession= “HR”;
                        }
                        else if (row==3)
                        {
                            profession = “IT”;
                        }

                        writer.Write(profession);
                        writer.Write(” “);
                        writer.Write(contractorProfessions[row,column]);
                        writer.WriteLine();
                        writer.Flush();//must have () after Flush command
                         }
            
        
                    }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, “Error”);
            }

            finally //forces the writer to close regardless of an exception being thrown or not.
                //needed to release the file handler due to finite resources.
            {
                writer.Close();
            }
        }
    }
}

For a laugh

/* Usually data inserted here.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace sample
{
class Program
{
static void Main(string[] args)
{
string[] definitions = new string[20];

// Array covering all the instructed data

definitions[0] = “Used to create objects on the heap and invoke constructors.”;
definitions[1] = “The finally block is useful for cleaning up any resources allocated in the try block.”;
definitions[2] = “The Haep is the section of memory in which reference types are allocated”;
definitions[3] = “The Stack is the section of memory in which Value types are allocated”;
definitions[4] = “Virtual memory is a technique by which the system can swap pieces of memory of the machines hard disk to create an illusion for the CPU that it Has memory”;
definitions[5] = “A value Type is a Microsoft .NET type containing actual data rather than a reference to data stored elsewhere in memory.”;
definitions[6] = “A Reference Type is a variable which holds a refernce(pointer) to data rather than containing the actual data.”;
definitions[7] = “An Arrary is a data structure used to store lists of values.”;
definitions[8] = “The Main() Method is a method required by C# to run both Console and Windows From programs.”;
definitions[9] = “The Switch statement is a control statement that handles multiple selections and enumerations by passing control to one of the case statements within it’s body.”;
definitions[10] = “The Queue Class represents a first-in, first-out colection fo objects”;
definitions[11] = “int.Parse() converts a streing of interger characters into a primitive int data type”;
definitions[12] = “The ToString() Method converts a primitive type or object into a string representation.”;
definitions[13] = “decimal is a primitive type that is used to store currency data.”;
definitions[14] = “A Primitive Type is predefined by the language and is named by a reserved keyword”;
definitions[15] = “The Call Stack refers to the fact that when a method is called, it’s added to the top of the stack. When a method returns to its caller,it’s removed from the stack”;
definitions[16] = “Local Variables are variables that are created in a method or code block for the lifetime of that method or code block. They are not visible to any other part of the program”;
definitions[17] = “[] is referred to as an array subscripts. They are used to create array size and access array members.”;
definitions[18] = “{} are used to encapsulate blocks of code such as methods and conditional statements”;
definitions[19] = ” The Scope of a name (e.g. a variable or method) is the region of a program text within which it is possible to refer to the entity declared by name.”;

int option = 0;

do
{
try
{
Console.WriteLine(“IT Tutor C~ Course E-Learnign Prototype”);
Console.WriteLine(“Select an option”);
Console.WriteLine(“1. New”);
Console.WriteLine(“2. finally”);
Console.WriteLine(“3. Heap”);
Console.WriteLine(“4. Stack”);
Console.WriteLine(“5. Virtual memory”);
Console.WriteLine(“6. Value Type”);
Console.WriteLine(“7. ReferenceType”);
Console.WriteLine(“8. Array”);
Console.WriteLine(“9. main() method”);
Console.WriteLine(“10. Switch”);
Console.WriteLine(“11. Queue Class”);
Console.WriteLine(“12. int.Parse() Method”);
Console.WriteLine(“13. ToString() Method”);
Console.WriteLine(“14. decimal”);
Console.WriteLine(“15. Primitive type”);
Console.WriteLine(“Call Stack”);
Console.WriteLine(“Local Variables”);
Console.WriteLine(“18. []”);
Console.WriteLine(“19. {}”);
Console.WriteLine(“20. Scope”);
Console.WriteLine(“21. Exit”);

option = int.Parse(Console.ReadLine());

if (option < 1 && option > 21)
{
throw new ArgumentOutOfRangeException(“Invalid option Selection.”);
}

if (option == 21)
{
Environment.Exit(0);
}

Console.WriteLine();
Console.WriteLine(definitions[option – 1]);
Console.ReadLine();

}
catch (FormatException fEx)
{
Console.WriteLine(fEx.Message);
Console.ReadLine();
}

catch (ArgumentOutOfRangeException aoorEx)
{
Console.WriteLine(aoorEx.Message);
Console.ReadLine();
}

catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.ReadLine();
}

}
while (option != 21);
}
}
}