Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Problem with Generic method and T: IComparable<T> implementati

$
0
0
I need to implement a generic search function in my code. If the user inputs a number whether it be integer or double, it needs to search the appropriate array for a match. I have two arrays (integer, double). I am supposed to write a generic search function to handle both of these. The user can input an integer or a double and then search the array to see if that number exists in the array. I'm supposed to use T : IComparable<T>, but it doesn't tell me how to implement it. I really am at a complete loss for this one. My book is of no help and what I have found online isn't of much help either. I greatly appreciate any tips, suggestions, or advice offered!

    public partial class MainForm : Form
    {
        //declare arrays
        int[] IntArray = new int [10];
        double[] DblArray = new double[10];

        public MainForm()
        {
            InitializeComponent();
        }

        private void BtnInt_Click(object sender, EventArgs e)
        {
            //Displays a random integer number to the user
            //Declare variables
            int rndInt;
            int minimum = 1;
            int maximum = 10;

            //Declare instance of random
            Random rnd = new Random();

            //Generate random number
            rndInt = rnd.Next(minimum, maximum);

            //Show random number
            tbInt.Text = rndInt.ToString();            
        }

        private void BtnDbl_Click(object sender, EventArgs e)
        {
            //Displays a random double number to the user
            //Declare variables
            double rndDbl;
            double minimum = 1.0;
            double maximum = 10.0;

            //Declare instance of random
            Random rnd = new Random();

            //Generate random number
            rndDbl = rnd.NextDouble() * (maximum - minimum) + minimum;
            rndDbl = Math.Round(rndDbl, 2);

            //Show random number
            tbDbl.Text = rndDbl.ToString();
        }

        private void BtnPopIntAry_Click(object sender, EventArgs e)
        {
            //Populates an integer array with random integers
            //Declare variables
            int minimum = 1;
            int maximum = 10;
            LblDisplay.Text = "";

            //Declare instance of random
            Random rnd = new Random();

            //Populate Array
            for (int i = 0; i < 10; i++)
            {
                //Generate random number
                IntArray[i] = rnd.Next(minimum, maximum);
                LblDisplay.Text = LblDisplay.Text + "  " + IntArray[i];
            }
        }
        
        private void BtnPopDblAry_Click(object sender, EventArgs e)
        {
            //Populates a Double array with random double numbers
            //Declare variables
            double minimum = 1.0;
            double maximum = 10.0;
            LblDisplay.Text = "";

            //Declare instance of random
            Random rnd = new Random();

            //Populate Array
            for (int i = 0; i < 10; i++)
            {
                //Generate random number
                DblArray[i] = rnd.NextDouble() * (maximum - minimum) + minimum;
                DblArray[i] = Math.Round(DblArray[i], 2);
                LblDisplay.Text = LblDisplay.Text + "  " + DblArray[i];
            }
        }

        private void BtnIntSearch_Click(object sender, EventArgs e)
        {
           //Convert user input to Integer
           int searchNum = Convert.ToInt32 (tbIntSearch);

           //Search array for match
           bool result = SearchArray(IntArray, searchNum);


        }

        private bool SearchArray(T[] inputArray, num)
        {

        }
      }


Viewing all articles
Browse latest Browse all 51036

Trending Articles