none
how can console to windows forms RRS feed

  • Question

  • i have this code here that is finding Median and mode but its in console and i would like to use it in win forms.

        

     public static decimal GetMedian(this IEnumerable<int> source)
            {
                // Create a copy of the input, and sort the copy
                int[] temp = source.ToArray();
                Array.Sort(temp);
                int count = temp.Length;
                if (count == 0)
                {
                    throw new InvalidOperationException("Empty collection");
                }
                else if (count % 2 == 0)
                {
                    // count is even, average two middle elements
                    int a = temp[count / 2 - 1];
                    int b = temp[count / 2];
                    return (a + b) / 2m;
                }
                else
                {
                    // count is odd, return the middle element
                    return temp[count / 2];
                }
            }
            public static int GetMode(this IEnumerable<int> list)
            {
                // Initialize the return value
                int mode = default(int);
                // Test for a null reference and an empty list
                if (list != null && list.Count() > 0)
                {
                    // Store the number of occurences for each element
                    Dictionary<int, int> counts = new Dictionary<int, int>();
                    // Add one to the count for the occurence of a character
                    foreach (int element in list)
                    {
                        if (counts.ContainsKey(element))
                            counts[element]++;
                        else
                            counts.Add(element, 1);
                    }
                    // Loop through the counts of each element and find the 
                    // element that occurred most often
                    int max = 0;
                    foreach (KeyValuePair<int, int> count in counts)
                    {
                        if (count.Value > max)
                        {
                            // Update the mode
                            mode = count.Key;
                            max = count.Value;
                        }
                    }
                }
                return mode;


               
    Wednesday, July 24, 2019 8:32 PM

All replies

  • Hello,

    You should be able to do so as follows

    private void button1_Click(object sender, EventArgs e)
    {
        IEnumerable<int> source = new List<int>() {10,23};
        var result = source.GetMedian();
    }

    using System;
    using System.Collections.Generic;
    using System.Linq;
    
    namespace WindowsFormsApp1
    {
        public static class Sample
        {
            public static decimal GetMedian(this IEnumerable<int> source)
            {
                // Create a copy of the input, and sort the copy
                var temp = source.ToArray();
                Array.Sort(temp);
                int count = temp.Length;
                if (count == 0)
                {
                    throw new InvalidOperationException("Empty collection");
                }
                else if (count % 2 == 0)
                {
                    // count is even, average two middle elements
                    int a = temp[count / 2 - 1];
                    int b = temp[count / 2];
                    return (a + b) / 2m;
                }
                else
                {
                    // count is odd, return the middle element
                    return temp[count / 2];
                }
            }
    
            public static int GetMode(this IEnumerable<int> list)
            {
                // Initialize the return value
                int mode = default(int);
                // Test for a null reference and an empty list
                if (list != null && list.Count() > 0)
                {
                    // Store the number of occurences for each element
                    Dictionary<int, int> counts = new Dictionary<int, int>();
                    // Add one to the count for the occurence of a character
                    foreach (int element in list)
                    {
                        if (counts.ContainsKey(element))
                            counts[element]++;
                        else
                            counts.Add(element, 1);
                    }
    
                    // Loop through the counts of each element and find the 
                    // element that occurred most often
                    int max = 0;
                    foreach (KeyValuePair<int, int> count in counts)
                    {
                        if (count.Value > max)
                        {
                            // Update the mode
                            mode = count.Key;
                            max = count.Value;
                        }
                    }
                }
    
                return mode;
            }
        }
    }


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange


    Wednesday, July 24, 2019 8:36 PM
    Moderator
  • Thanks Karen
    Wednesday, July 24, 2019 10:09 PM
  • But am still having an error since my win Forms is using 
    public partial class Form1 : Form
    and whilst the one you provided is using
    public static 

    Wednesday, July 24, 2019 10:33 PM
  • Here is a working example which I compiled and ran and worked as expected. Note the class Sample is static because the methods are language extension methods which must be in a static class.

    https://1drv.ms/u/s!AtGAgKKpqdWjjT_MXSJFjzgh5rHl?e=pVYv8k


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Wednesday, July 24, 2019 11:48 PM
    Moderator
  • Hi Mart nta,

    Thank you for posting here.

    For your question, you want to use the above code in windows forms.

    You could try the following code.

     public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Button1_Click(object sender, EventArgs e)
            {
                int[] arr = { 1, 2, 3, 4, 5 };
                decimal m =GetData.GetMedian(arr);
                textBox1.Text = m.ToString();
                List<int> list = new List<int>() {22,23,54,55,88,99,45 };
                int n = GetData.GetMode(list);
                textBox2.Text = n.ToString();
    
            }
           
        }
        public static class GetData
        {
    
            public static decimal GetMedian(this IEnumerable<int> source)
            {
                // Create a copy of the input, and sort the copy
                int[] temp = source.ToArray();
                Array.Sort(temp);
                int count = temp.Length;
                if (count == 0)
                {
                    throw new InvalidOperationException("Empty collection");
                }
                else if (count % 2 == 0)
                {
                    // count is even, average two middle elements
                    int a = temp[count / 2 - 1];
                    int b = temp[count / 2];
                    return (a + b) / 2m;
                }
                else
                {
                    // count is odd, return the middle element
                    return temp[count / 2];
                }
            }
            public static int GetMode(this IEnumerable<int> list)
            {
                // Initialize the return value
                int mode = default(int);
                // Test for a null reference and an empty list
                if (list != null && list.Count() > 0)
                {
                    // Store the number of occurences for each element
                    Dictionary<int, int> counts = new Dictionary<int, int>();
                    // Add one to the count for the occurence of a character
                    foreach (int element in list)
                    {
                        if (counts.ContainsKey(element))
                            counts[element]++;
                        else
                            counts.Add(element, 1);
                    }
                    // Loop through the counts of each element and find the 
                    // element that occurred most often
                    int max = 0;
                    foreach (KeyValuePair<int, int> count in counts)
                    {
                        if (count.Value > max)
                        {
                            // Update the mode
                            mode = count.Key;
                            max = count.Value;
                        }
                    }
                }
                return mode;
            }
    
        }

    Result:

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Thursday, July 25, 2019 2:37 AM
    Moderator