Category: Programming



This example will illustrate how to write one event handler to multiple objects.  This example utilizes the checkbox objects.  In the app, the user has an option to check up to four checkboxes.  Each time he checks one, the total dollar value for each item the user is checking is displayed on a label. 

An alternative would have been to place the code inside a button object.  This would also provide the total in dollars for the checkboxes checked but not until the button was pressed.  This application illustrates running totals based on check box clicks.

 

Two files are attached.  A flash movie file illustrating the steps and a zip file containing the code.  I recommend you oOpen the .swf file in a new window.

System.Array class methods


BinarySearch() – Class method.  Searches a one dimensional array for a value.  It returns the location or negative value if not found.

int i = Array.BinarySearch (arrayName, value);
Console.Write(i);

Clear() – Class method.  Sets elemnts in the array to zero, false or null depending ont the element type starting at x ending at y positions.

Array.Clear(arrayName, 2, 4);

Clone() – Creates a copy of the array.  Returns an object.

Copy() - Copies a section of one array to another array.

Array.Copy (a1, startIndexa1,  a2, startIndexa2, length)
Array.Copy(array1, 0, array2, 0, 5)

TBC . . .

Signature of a Method – C#


A signature of a method consists of:

  1. The name of the method (e.g. Main(), ComputeCost(), OtherUserDefinedMethodName())
  2. Modifiers (public, public static, private, private static, etc…)
  3. Types of its formal parameters

The formal parameters are inside the method parenthesis.  Examples include:

  • int numStudents
  • decimal cashBalance
  • double arg1
  • string arg2

Unlike delegates in C#, the return type IS NOT part of a method’s signature.  Return types include void or one type of data object such as int, string, array and so on.

If the return type is not void, the method must include the return keyword followed by a value that is of the data object declared as the return type.

Example:

private double MileageCost(int miles, double rate)

{

       return miles * rate;

}

 


As soon as you run this program you will see a prompt asking you to enter a value.  That is because random values from 1 – 20 were already assigned to the declared array.  Now, you are simply ready to ask the program if the value that you enter is present in the array or not.  In addition to telling you if it is there, it will also count the number of times a value appears if it appears. 

 

Note, you can generate a value greater than 20, by replacing the number 20 within rn.Next(20) to higher value.

 

using System;

using System.Collections.Generic;

using System.Text;

 

namespace pe7_7

{

    /*This program will utilize the

     * array binary search.  It will

     * also utlize sort.  */

 

    class BinarySearch

    {

 

        static void Main(string[] args)

        {

            int[] fVal = new int[15];

            Random rn = new Random();

            int ui;

 

            for(int k=0; k<15; k++)

            {

                fVal[k] = rn.Next(20);

            }

 

            Array.Sort(fVal);

            Console.Write("Search for a value: ");

            ui = Convert.ToInt32(Console.ReadLine());

           

            //the UserSearch() method uses the BinarySearch method

            //from the Array class to determine if the value exists

           

            UserSearch(ui, fVal);

 

            Console.ReadLine();

        }

 

        static void UserSearch(int ui, int[] fVal)

        {

            int counter = 0;

            foreach (int i in fVal)

            {

                if (i == ui)

                {

                    counter++;

                }

               

            }//end foreach

 

            if (Array.BinarySearch(fVal, ui) > -1)

            {

                Console.Write("Using Binary Search, the value {0} exists in the array. \n" +

                              "It appears {1} time(s) in the array.", ui, counter);

            }

            else

            {

                Console.Write("Using Binary Search, the value {0} does not exists in array.", ui);

            }

 

        }

    }

}

 

 


Using ArrayList we will create a program that will accept any number of grades, then except for the highest and lowest value, it will output what percent of 100 of each grade is.  This exercise probably does not have any practical use or value but it makes use of the following techniques:

1.)    ArrayList Sort

2.)    ArrayList RemoveAt

3.)    ArrayList Item (During the concatention of string in loop)

4.)    Use of the foreach loop and while loop

5.)    Demostrates passing ArrayList types from one method to another

 

using System;

using System.Collections;

using System.Collections.Generic;

using System.Text;

 

namespace pe7_8

{

    class Homework

    {

        static void Main(string[] args)

        {

            ArrayList grades = new ArrayList();

            int sentinel = 0;

            Console.Write("Enter the students grades: \n");

            Console.Write("Enter -99 to exit.");

 

            while (sentinel != -99)

            {

                Console.Write("Enter another grade: ");

                sentinel = Convert.ToInt32(Console.ReadLine());

                if (sentinel != -99)

                {

                    grades.Add(sentinel);

                }

                else

                {

                    break;

                }

            }

 

            PercentageOf(grades);

            Console.ReadLine();

 

        }//end Main method

 

        static void PercentageOf(ArrayList grades)

        {

            ArrayList gradePct = new ArrayList();

            int counter = 0;

            double value;

            string messageOut = "\n \nWith the exception of the highest and lowest \n" +

                                "grade, the percent out of 100 for each grade is: \n";

           

            grades.Sort();

            grades.RemoveAt(0);

            grades.Reverse();

            grades.RemoveAt(0);

 

            foreach(int k in grades)

            {

                value = Convert.ToDouble(k/100.00);

                gradePct.Add(value);

                messageOut += "Grade " + k + " is " + gradePct[counter] + " percent of 100. \n";

                counter++;

            }

 

            Console.Write(messageOut);          

        }

    }//end class Homework

}