We have answer of your question!

100% solved queries, no empty question

Question: Can't Call arrays in main method


0

I'm practicing methods, but the problem is I want to separate inputting and sorting, the display method will be the main, I'm having trouble fixing this calling from other class.

This is my script :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CaseProblem
{
    class Method
    {
        static void MethodInput()
        {
            int[] array = new int[5];
            int i;
            // loop for accepting values in array
            for (i = 0; i < 5; i++)
            {
                Console.Write("Enter number:\t");
                array[i] = int.Parse(Console.ReadLine());
            }
        }
        public static void MethodSort()
        {
            foreach (int i in array)
            {
                Console.Write(" {0}", i);
            }
        }
    }
    class Program
    {
        static void Main(int[]array)
        {
            //sorting array value;
            Array.Sort(array); //use array's sort function

            Method.MethodSort(array);
            Console.ReadLine();
        }
    }
}

Thank you for your help

Question author Patrickman | Source

Answer


1


First of all Main() ( or "EntryPoint" as we should call it ) cannot have int[] as an input parameter but string[] instead you should learn basics of programming before starting to actually code something.

Second thing :

I want to separate inputting and sorting

You can create an object called Input

public class Input
{
    public static void Write(string message)
    {
        Console.WriteLine(message);
    }

    public static int? ReadInt(string reason)
    {
        Write(reason);
        string userInput = Console.ReadLine();
        int parsed = 0;
        if(int.TryParse(userInput, out parsed))
            return (int?)parsed;

        return null;
    }
}

This will be your "InputLogic" which you can use as Input.ReadInt("Please specify your age: ");

Next you can make an Operations object :

public class Operations
{
    public void Display(int[] arr)
    {
        foreach(int i in arr)
        {
            // and since you have "Input" class that can display things
            Input.Write(i.ToString());
        }
    }

    public void Sort(ref int[] arr)
    {
        Array.Sort(arr);
    }
}

Now the last thing is to combine it within your Program

class Program
{
    static void Main(string[] args)
    {
        int[] arr = new int[5];
        for(int i = 0; i < arr.Length; i++)
        {
            int? input = null;
            while( !( input = Input.ReadInt("Give me number") ).HasValue ) { }
            arr[i] = input.Value;
        }

        Operations op = new Operations();
        op.Display(arr);
        op.Sort(ref arr);
        op.Display(arr);
    }
}
Answer author M-rogalski

Tickanswer.com is providing the only single recommended solution of the question Can't Call arrays in main method under the categories i.e c# , arrays , sorting , methods , . Our team of experts filter the best solution for you.

Related Search Queries:


You may also add your answer

Thanks for contributing an answer to Tick Answer!