locked
How to use user input in a function? RRS feed

  • Question

  • Hello Guys!

    I was wondering if this code i have inside main function. Is it possible to make a function so that I

    dont need to have it inside main? How does it work? I dont want Console.ReadLine inside of that function.

    Going crasy...

    Kind regards


    // My function that delivers max value in array. static int maxValue(int[]maxValue) { int sum = maxValue.Max(); return sum; } static void Main(string[] args) { int[] myArray = new int[2]; // How can i put this for-loop into a function? // I dont want this inside main. I want user input // to be inside a function. for (int i = 0; i < myArray.Length; i++) { Console.WriteLine("Type in a number."); myArray[i] = int.Parse(Console.ReadLine()); } Console.WriteLine("This is the Highest value {0}",maxValue(myArray)); Console.ReadKey(); }


    Friday, November 1, 2013 2:28 PM

Answers

  • Here your go

    // My function that delivers max value in array.
            static int maxValue(int[]maxValue)
            {
                int sum = maxValue.Max();
                return sum;
            }
     
    static int[] GetValues()
    {
       int[] myArray = new int[2];
       for (int i = 0; i < myArray.Length; i++)
       {
          Console.WriteLine("Type in a number.");
          myArray[i] = int.Parse(Console.ReadLine());
       }
    }
    
    static void DisplayResult(int[] arr)
    {
       Console.WriteLine("This is the Highest value {0}",maxValue(arr));
       Console.ReadKey();
    }
    
            static void Main(string[] args)
            {
                int[] myArray = GetValues();
                DisplayResult(myArray);
            } 

    Hope this helps!


    A.m.a.L Hashim
    Microsoft Most Valuable Professional
    My Blog - Dot Net Goodies

    • Proposed as answer by Tonchi91 Friday, November 1, 2013 4:33 PM
    • Marked as answer by Caillen Tuesday, November 12, 2013 7:11 AM
    Friday, November 1, 2013 3:40 PM

All replies

  • Here your go

    // My function that delivers max value in array.
            static int maxValue(int[]maxValue)
            {
                int sum = maxValue.Max();
                return sum;
            }
     
    static int[] GetValues()
    {
       int[] myArray = new int[2];
       for (int i = 0; i < myArray.Length; i++)
       {
          Console.WriteLine("Type in a number.");
          myArray[i] = int.Parse(Console.ReadLine());
       }
    }
    
    static void DisplayResult(int[] arr)
    {
       Console.WriteLine("This is the Highest value {0}",maxValue(arr));
       Console.ReadKey();
    }
    
            static void Main(string[] args)
            {
                int[] myArray = GetValues();
                DisplayResult(myArray);
            } 

    Hope this helps!


    A.m.a.L Hashim
    Microsoft Most Valuable Professional
    My Blog - Dot Net Goodies

    • Proposed as answer by Tonchi91 Friday, November 1, 2013 4:33 PM
    • Marked as answer by Caillen Tuesday, November 12, 2013 7:11 AM
    Friday, November 1, 2013 3:40 PM
  • Thanks!

    If i would do something like this on a windowsform and using input from textboxes, do I do the same and use textbox.text inside of the foor-loop?
    Friday, November 1, 2013 8:54 PM
  • Yes the logic is same, but you should decide when you want to accept the input. For example in case of console you can use the enter key to specify the end of a word. In forms application to replicate this behavior you should handle the key press event of the textbox control.

    A.m.a.L Hashim
    Microsoft Most Valuable Professional
    My Blog - Dot Net Goodies

    Saturday, November 2, 2013 1:45 PM