How to pass an object as a parameter to another class method

Asked

Viewed 813 times

1

I am developing an object-oriented agenda and recording in a text file.

I’m finding the following problem:

when saved a contact it is passing to another object as if it were a pointer

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


namespace AgendaConsole
{
    class Program
    {
        private static bool sair = false;

        static void Main(string[] args)
        {
            Contato C = new Contato();
            Agenda A = new Agenda();

            if (A.Inicia())
            {
                Console.WriteLine("Agenda Carregou!");
            }

            do
            {
                string resposta;
                Console.Clear();
                Console.WriteLine("***AGENDA***");
                Console.WriteLine("1- Cadastrar");
                Console.WriteLine("2- Consultar");
                Console.WriteLine("3- Alterar");
                Console.WriteLine("4- Excluir");
                Console.WriteLine("5- Sair");
                resposta = Console.ReadLine();
                switch (resposta)
                {
                    case "1":

                        Console.Clear();
                        Console.WriteLine("Nome:");
                        C.setNome(Console.ReadLine());
                        Console.WriteLine("Email:");
                        C.setEmail(Console.ReadLine());
                        Console.WriteLine("Telefone:");
                        C.setTelefone(Console.ReadLine());
                        A.adicionaContato(C);

                        Console.WriteLine("Pressione qualquer tecla para voltar ao menu.");
                        Console.ReadKey();
                        break;
                    case "5":
                        sair = true;
                        break;

                }
            } while (!sair);
        }
    }
}

In line A.addendContact(C);

 public void adicionaContato(Contato C)
        {
            this.Contatos[posicao] = C;
            posicao++;
        }

always end up replicating and adding at position 0 and in which should even add duplicate contact, in the added callContact it is already with object in the agenda.

Updating ...

classe Agenda

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

namespace AgendaConsole
{
    class Agenda
    {
        private Contato[] Contatos = new Contato[100];
        GravaArquivo G = new GravaArquivo();
        int posicao = 0;

        public bool Inicia()
        {
            for (int i = 0; i < 100; i++)
            {
                this.Contatos = G.CarregaAgenda();
            }
            return true;
        }

        public Contato Busca(string b)
        {
            Contato C = new Contato();
            for (int i = 0; i < 100; i++)
            {
                if (b.Equals(this.Contatos[i].GetNome()))
                {
                    C = Contatos[i];
                }
            }
            return C;
        }

        public bool Salvar()
        {
            G.GravaAgenda(Contatos);

            return true;
        }

        public int buscaPosicao()
        {
            int cont = 0;
            for (int i = 0; i < 100; i++)
            {
                if (this.Contatos[i].GetNome().Equals(""))
                {
                    return cont;
                }
                else
                {
                    cont++;
                }
            }
            return cont;
        }

        public void adicionaContato(Contato C)
        {
            posicao=buscaPosicao();
            this.Contatos[posicao] = C;            
        }
    }
}
  • 1

    The problem is not this, and is elsewhere, we know what we are doing. In fact this code does not even look like C#, even if it is. There must be several mistakes.

  • 1

    Where is the Schedule class?

  • Try to make the other case of the switch available to check the code completely.

  • @Luizaugusto I gave a formatted one and added a minimum complement in the code of your question so that it makes some sense. If you still want help in this matter add your class Agenda and the remaining code relevant to the question.

  • I added the code of the Agenda class

  • I resolved passing attributes and not whole object

Show 1 more comment
No answers

Browser other questions tagged

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