I'm developing an object-oriented calendar and writing to a text file.
I'm encountering the following problem:
When saved a contact 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.addressContact (C);
public void adicionaContato(Contato C)
{
this.Contatos[posicao] = C;
posicao++;
}
It always ends up replicating and adding in position 0 and in that it should even add duplicating the contact, in the call addContact it already is 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;
}
}
}