Vector receives method return

Asked

Viewed 326 times

1

I am assembling a program in C# and created a method called Supply that receives an array called a number and returns the same. The idea is that the supply function creates random values, fill in the vector and return it filled to the main function (I haven’t done this value creation part yet), the function is being called in the Main.

The problem: is accusing that the line in which I call the method in the Main is wrong, I’ve tried many ways, but so far nothing has worked.

I don’t know if I declared 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();
            }
        }
    }
  • Say what’s wrong, what’s the line.

  • 'number = Supply(number);'

  • Your retorno++; is out of the whileso it doesn’t work.

  • What’s the mistake??

  • tidied but continues to signal error in the @Marlon Tiedt method call

  • There I got it! had to put Static in the method statement

Show 1 more comment

2 answers

4


The problem on the specific line is that the method Main() is static and the Fornecimento() it is not, you cannot 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 the Main() need to be public. After tidying this will compile, but will not run right because the code is full of other errors.

I improved the code, eliminating some errors, some bad practices, modernized it. There are things that could get better. But tired :) I recommend taking advantage of this and the tips in the comments to improve more.

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++;
            }
        }
    }
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

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

  • Thank you so much I’ve seen so far,!!

1

  • Thank you so much for your help!!! :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.