Vector gets method return

1

I'm putting together a program in C # and I've created a method called Supply that receives a vector called number and returns it. The idea is that the supply function creates random values, populates the vector and returns it filled for the main function (I have not done this part of creating values yet), the function is being called in Main .

The problem: I am accusing that the line in which I call the method in Main is wrong, I have tried in several ways, but so far nothing has worked out.

I do not know if I declare the line right:

ERROR LINE -: number = Supply (number);

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

    namespace ConsoleApplication1
    {
        class Program
        {

            /*---------------------------------------------------
                            Métodos do programa
            ---------------------------------------------------*/

            //Método de fornecimento de numeros
            int[] Fornecimento(int[] numero)
            {
                int opcao;
                //apresentação do tipo de opcao
                Console.Clear();
                Console.Write("*****************************************************************");
                Console.Write("\n*\t\t\t\t\t\t\t\t*");
                Console.Write("\n*\t\tFORNECIMENTO DE NUMEROS\t\t\t\t*");
                Console.Write("\n*\t\t\t\t\t\t\t\t*");
                Console.Write("\n*****************************************************************");
                Console.Write("\n\n\tO que deseja fazer: \n\n\t=>1-Fornecer os numeros \n\t=>2-Gerar numeros automaticamente \n\n\tEscolha uma opcao: ");
                opcao = Convert.ToInt32(Console.ReadLine());

                if (opcao == 1)
                {

                }

                return (numero);
            }


            /*---------------------------------------------------
                        Método principal do programa
            ---------------------------------------------------*/
            static void Main(string[] args)
            {
                //declaração de variáveis
                int[] numero = new int[500000];
                int retorno = 1, tipo = 0;

                while (retorno == 1)
                {
                    //comando limpa a tela
                    Console.Clear();
                    //apresentação do programa
                    Console.Write("*****************************************************************");
                    Console.Write("\n*\t\t\t\t\t\t\t\t*");
                    Console.Write("\n*\t\t\tALGORITMO DE ORDENACAO\t\t\t*");
                    Console.Write("\n*\t\t\t\t\t\t\t\t*");
                    Console.Write("\n*****************************************************************");

                    //menu de escolha do tipo de algoritmo de ordenação(loop repete até que um tipo válido seja inserido)
                    while ((tipo != 1) && (tipo != 2) && (tipo != 3))
                    {
                        Console.Write("\n\n\tQue tipo de algoritmo deseja utilizar: \n\n\t1-Inserction sort \n\t2-Selection sort \n\t3-Bubble sort \n\n\tEscolha uma opção=> ");
                        tipo = Convert.ToInt32(Console.ReadLine());
                    }

                    numero = Fornecimento(numero);

                }
                retorno++;
                //Environment.Exit(exitCode);
                Console.ReadKey();
            }
        }
    }
    
asked by anonymous 30.05.2016 / 20:19

2 answers

4

The problem in the specific line is that the Main() method is static and Fornecimento() is not, you can not call an instance method in a static method. Both need to be static, so just put a static there in the method and this problem will solve.

There is also the problem that Main() needs to be public. After sorting this will compile, but will not run right because the code is full of other errors.

I have improved the code, eliminating some errors, some bad practices, modernized it. There's something that could get better. But I'm tired :) I recommend to take advantage of this and the tips in the comments to improve.

using System;
using static System.Console;

namespace ConsoleApplication1 {
    public class Program {
        static int[] Fornecimento(int[] numero) {
//          Clear();
            WriteLine("*****************************************************************");
            WriteLine($"*{new String(' ', 63)}*");
            WriteLine($"*{new String(' ', 16)}FORNECIMENTO DE NUMEROS{new String(' ', 24)}*");
            WriteLine($"*{new String(' ', 63)}*");
            WriteLine("*****************************************************************");
            WriteLine($"\n{new String(' ', 8)}O que deseja fazer: \n\n{new String(' ', 8)}=>1-Fornecer os numeros \n{new String(' ', 8)}=>2-Gerar numeros automaticamente \n\n{new String(' ', 8)}Escolha uma opcao: ");
            int opcao = Convert.ToInt32(ReadLine()); //espero que esta variável vá ser usada em outro lugares
            if (opcao == 1) {
                //espero que vá fazer alguma aqui
            }
            return numero;
        }

        public static void Main(string[] args) {
            int[] numero = new int[500000];
            int retorno = 1, tipo = 0; //tipo precisa ser declarada aqui mesmo? no momen to não
            while (retorno == 1) { //este while não faz o menor sentido
//              Clear(); //por alguma razão aqui dá problema executar, mas pode descomentar isso
                WriteLine("*****************************************************************");
                WriteLine($"*{new String(' ', 63)}*");
                WriteLine($"*{new String(' ', 16)}ALGORITMO DE ORDENACAO{new String(' ', 25)}*");
                WriteLine($"*{new String(' ', 63)}*");
                WriteLine("*****************************************************************");
                while (tipo >0 && tipo < 4) {
                    //isso é muito gambo, o ideal seria fazer cada linha em um comando próprio, arumei um pouco mas desisti de fazer tudo
                    Write($"\n\n{new String(' ', 8)}Que tipo de algoritmo deseja utilizar: \n\n{new String(' ', 8)}1-Inserction sort \n{new String(' ', 8)}2-Selection sort \n{new String(' ', 8)}3-Bubble sort \n\n{new String(' ', 8)}Escolha uma opção=> ");
                    tipo = Convert.ToInt32(ReadLine()); //esta convers]ao é insegura, prefira um int.TryParse()
                }
                Fornecimento(numero);
                retorno++;
            }
        }
    }
}

See how you got on dotNetFiddle and on CodingGround >.

About using TryParse() . read everything, follow the links . Especially the Differences between Parse vs TryParse

    
30.05.2016 / 20:26
1

Adjust your code and put it in: link

    
30.05.2016 / 20:31